home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / others / z80_201.zip / Z80.DOC < prev    next >
Text File  |  1993-05-03  |  138KB  |  2,643 lines

  1. Sinclair ZX Spectrum Emulator 'Z80' v2.01  -  3/5/93  -  by G.A. Lunter
  2.  
  3.  
  4.  
  5. Contents:
  6.  
  7.     1.    INTRODUCTION, REGISTRATION, GENERAL INFORMATION
  8.     1.1   Some general remarks
  9.     1.2   Registering - sounds interesting!
  10.     1.3   Other emulators
  11.     1.4   Thanks
  12.  
  13.     2.    THE EMULATOR
  14.     2.1   Starting the emulator
  15.     2.2   Using the emulator
  16.     2.3   Emulation of the Keyboard, Screen and Beeper
  17.     2.4   Using the Tape
  18.     2.5   Using the Microdrive
  19.     2.6   Using the RS232 channel
  20.     2.7   Joysticks
  21.     2.8   Transferring programs
  22.     2.9   Converting file formats - the utility CONVERT
  23.     2.10  The utilities Z802TAP and TAP2TAPE
  24.  
  25.     3.    THE SAMRAM
  26.     3.1   Basic extensions
  27.     3.2   NMI software
  28.     3.3   The built-in monitor
  29.  
  30.     4.    THE SPECTRUM
  31.     4.1   The Spectrum
  32.     4.2   The Interface I
  33.     4.3   The Spectrum 128
  34.  
  35.     5.    TECHNICAL INFORMATION
  36.     5.1   The Spectrum
  37.     5.2   The Interface I
  38.     5.3   The SamRam
  39.     5.4   The Z80 microprocessor
  40.     5.5   File formats
  41.  
  42.  
  43.  
  44.  
  45. 1. INTRODUCTION, REGISTRATION, GENERAL INFORMATION
  46.  
  47.  
  48. 1.1 Some general remarks
  49.  
  50.     This is the documentation for 'Z80', a Sinclair ZX Spectrum 48/128
  51.     emulator.  This program turns your PC into a Spectrum.  To make you
  52.     read on,...
  53.  
  54.       The program emulates a Spectrum 48K model 2 or 3 or Spectrum 128K,
  55.       is very, very compatible with the real machines, includes the
  56.       Interface I, supports Microdrives, tape files, the RS232 channel,
  57.       '128 sound through internal speaker or Adlib Compatible soundcard,
  58.       able to save and load every Spectrum program directly to and from
  59.       tape, even able to load speed-saved programs, supporting digital
  60.       and analogue PC joysticks and four common Spectrum joysticks, Z80
  61.       processor emulation including the R register, inofficial opcodes
  62.       and flags, accurate timing of individual instructions, control
  63.       over the emulated Spectrum's speed, and all that while requiring
  64.       only a PC-XT with 512K with CGA, Hercules, EGA or VGA; including
  65.       conversion programs to convert between various emulators' snapshot
  66.       formats and to read from Disciple and Plus D diskettes, to create
  67.       .PCX and .GIF files of Spectrum screen dumps, an English manual...
  68.  
  69.     There is much to tell and explain in this documentation.  First of all
  70.     the emulator itself must run, and uses your PC's resources.  It is not
  71.     really a demanding program, but there are some things that need
  72.     attention.  These technicalities are dealt with in section 2.1.
  73.  
  74.     Some general things about the emulator are explained in section 2.2. If
  75.     you read 2.1 and 2.2, you'll be able to do most of the things you
  76.     probably ever want to do.  But to exploit all of its possibilities (and
  77.     oh, it can do so much!), you will really have to read it all.
  78.  
  79.     The Spectrum has a number of ways to communicate with the outside world,
  80.     like the obvious keyboard and the screen, but also the microdrives, the
  81.     tape interface, the beeper, the sound chip of the Spectrum 128, the
  82.     Kempston joystick, and the RS232 channel of the Interface I and Spectrum
  83.     128.  All these channels can be used to communicate with PC channels in
  84.     some way; for instance the keyboard is connected to the PC keyboard
  85.     (sounds obvious) and the tape I/O can be routed to a file, as well as to
  86.     a physical tape recorder.  All these things are explained in the rest of
  87.     chapter 2.  Paragraph 8 of that chapter contains a number of suggestions
  88.     how to transfer Spectrum programs to the PC.
  89.  
  90.     For our own Spectrums Johan Muizelaar and I built a piece of hardware we
  91.     called the SamRam (which has nothing to do with the SAM coupe, by the
  92.     way!).  It contains a monitor program and software to make snapshots of
  93.     programs.  It's still very useful and I still use it a lot, although the
  94.     physical SamRam doesn't work anymore.  An explanation of its functions
  95.     is to be found in chapter 3.
  96.  
  97.     Some things peculiar to the Spectrum, not specific to this program but
  98.     useful to know are collected in chapter 4.  It contains for instance a
  99.     table of Spectrum keywords and the key combination to get it;
  100.     unfortunately this information is not printed on standard PC keyboards!
  101.  
  102.     There are some interesting, rather unknown technical facts about the
  103.     Spectrum that I discovered while debugging the emulator.  As much as I
  104.     could think of is contained in the final chapter.  You don't need to
  105.     read this chapter to use the emulator; if you don't find it interesting
  106.     then skip it, but I think programmers will like it.
  107.  
  108.     A remark about copyrights.  The source files are not public domain, and
  109.     you may not use them in other PC-based Spectrum emulators. Also, the
  110.     information in this documentation file, especially the info in the final
  111.     chapter (except for the file-format info in the final section) may not
  112.     be used for that purpose.  But you're free to use the info for Spectrum
  113.     emulators for other machines, provided that whenever you do so you
  114.     should name the source.
  115.  
  116.     For Spectrum software, utilities, other emulators for PC's as well as
  117.     other computers, and other Spectrum related software, you can call the
  118.     Spectrum Emulator support BBS in Groningen:
  119.  
  120.         Tatort BBS Groningen
  121.         050-264840
  122.         (+31-50-264840)
  123.         v22, v22bis, v32, v32bis, MNP2-5, v42, v42bis (300-14400 baud)
  124.  
  125.     At the time of writing the BBS is open 24 hours a day, but this is
  126.     subject to change.  Please try calling between 22:00 and 9:00 local time
  127.     first.
  128.  
  129.     If you have access to Internet, you can find several Spectrum emulators
  130.     in a directory of for instance wuarchive.wustl.edu (take a look in
  131.     /systems/sinclair and -/msdos/emulators) or nic.funet.fi.  And if you
  132.     want to get in touch with me, my email address is gerton@rcondw.rug.nl.
  133.  
  134.  
  135.  
  136.  
  137.  
  138. 1.2  Registering - sounds interesting!
  139.  
  140.     First of all, this program is shareware.  That means that you're
  141.     encouraged to give it away to others, but if you do be sure not to alter
  142.     the files.  Also please don't add things to the archive file. Shareware
  143.     means that you may try the program some time; if you like it you should
  144.     register for it.
  145.  
  146.     The shareware version of the emulator consists of the following files:
  147.  
  148.  
  149.         Z80.EXE      - The emulator
  150.         Z80.INI      - Default initialisation file
  151.         Z80.DOC      - This documentation file
  152.         Z80.TEX      - LaTeX documentation file, by Lars Koeller
  153.         Z80.PS       - PostScript documentation file, from the .TEX one
  154.         Z80.PIF      - Program Info File to run 'Z80' under Windows 3.1
  155.         Z80.ICO      - Windows icon
  156.         ROMS.BIN     - Various ROM images
  157.         LAYOUT.SCR   - Keyboard lay-out help screen
  158.         GETRS.COM    - Utility to receive blocks through RS232 lead
  159.         SAVESPEC.Z80 - Utility to send .Z80 files from Spectrum to PC
  160.         DIAGRAM.Z80  - Circuit diagram for tape interface, and calibration
  161.         Z80FAQ.DOC   - Frequently asked questions - and answers!
  162.         NEW.DOC      - The What's New file
  163.  
  164.  
  165.     The shareware version of the emulator program is not fully functional.
  166.     It cannot be slowed down, and it can't load programs from tape.  All
  167.     other functions work the same in both versions.  If you register, you
  168.     will receive the fully functional emulator together with the following
  169.     utilities:
  170.  
  171.         CONVERT  - a general conversion program: can list out BASIC and
  172.                    tranlate it back, produce .GIF or .PCX files from
  173.                    screendumps, translate Spectrum ASCII (CR) to PC ASCII
  174.                    (CR/LF), and some other things.
  175.         CONVZ80  - Translates various snapshot and tape formats of other
  176.                    Spectrum emulators into each other.  Can handle Arnt
  177.                    Gulbrandsen's (JPP) .SNA format, Pedro Gimeno's
  178.                    (VGASPEC and SPECTRUM) .SP format and Kevin J. Phairs'
  179.                    (SPECEM) .PRG format.  It can also handle tape files
  180.                    of SPECEM and L. Rindt and E. Brukner's emulator ZX.
  181.         DISCIPLE - Reads DISCiPLE and Plus D diskettes, both 3.5'' and
  182.                    5.25''.  It translates the 48K and 128K snapshot files
  183.                    to .Z80 snapshots, and ordinary files and screen
  184.                    snapshots to .TAP tape files.
  185.         Z802TAP  - Converts a .Z80 snapshot, 48K or 128K, to a .TAP file
  186.                    which can be loaded into the emulator and saved to tape
  187.                    by the next utility:
  188.         TAP2TAPE - Saves the contents of a .TAP file back to tape, to
  189.                    load it into an ordinary Spectrum.
  190.         Z80DUMP  - Shows the contents of the header of a .Z80 file.
  191.  
  192.  
  193.     You will also receive the source files of the emulator, the above
  194.     utilities and the SamRam, and you'll be kept informed about future
  195.     updates.
  196.  
  197.     The registration fee is 20 US$, or 15 British pounds, or 35 German
  198.     Marks, or 35 Dutch guilders, or some of your local (hard) currency of
  199.     about that amount.  Now there are several way to get the money to me.
  200.     In order of preference:
  201.  
  202.         1.  Simply send banknotes.
  203.         2.  From Europe, send a Eurocheque of HFL 35,--
  204.         3.  Send a postal money-order (Works fine from e.g. Italy and
  205.             Spain)
  206.         4.  Send a bank cheque.  Please add the equivalent of 20 Dutch
  207.             guilders, for that's the amount the banks charge for drawing
  208.             foreign cheques.
  209.  
  210.     If you're sending a Eurocheque, make sure you fill it in completely
  211.     (don't forget the number at the back!) and fill in 'Groningen' for the
  212.     place.  If you don't send Dutch currency, or don't fill it in
  213.     completely, or fill in a foreign city, the banks charge me fifteen to
  214.     twenty guilders to cash the cheque.
  215.  
  216.     For Dutch users, the fee is HFL 25,--.  In Nederland gaat het betalen
  217.     het gemakkelijkst via de giro.  Maak het bedrag over op giro 59.45.263
  218.     t.n.v. G.A. Lunter, Groningen.  Zorg er wel voor dat uw naam en adres
  219.     vermeld staan! (vooral als u Girotel gebruikt)
  220.  
  221.     Send the money, together with your name and address to:
  222.  
  223.                 Gerton Lunter
  224.                 P.O. Box 2535
  225.                 NL-9704 CM  Groningen
  226.                 The Netherlands
  227.  
  228.     You'll get the files on a 3.5'' DD disk by default, but you can also get
  229.     in on 5.25 inch disks if you want.
  230.  
  231.     Registrations can also be handled by B G Services in the UK if this is
  232.     more convenient.  The cost is the same (15 British pounds).  Payment can
  233.     be by cheque or postal order made payable to B G Services.  The address
  234.     is:
  235.  
  236.                 B G Services
  237.                 64 Roebuck Road
  238.                 Chessington
  239.                 Surrey KT9 1JX
  240.  
  241.     Telephone enquiries on 081 397 0763, Fax 081 391 0744.
  242.  
  243.     For registrations in the Czech Republic, I recommend to contact JIMAZ,
  244.     who will provide details on registering in the local currency:
  245.  
  246.                 JIMAZ s.r.o.
  247.                 Hermanova 37
  248.                 170 00 Praha 7
  249.                 phone: +42 2 379 498
  250.                 fax:   +42 2 378 103
  251.  
  252.  
  253.  
  254.  
  255. 1.3  Other emulators
  256.  
  257.     There are several other Spectrum emulators, both for the PC and other
  258.     computers.  The list below is partly due to Carlo Delhez (the QL
  259.     emulators) and partly copied from Arnt Gulbrandsen's documentation of
  260.     his JPP.  I don't think the list is complete, so if you know more
  261.     Spectrum emulators, for any computer, please let me know.
  262.  
  263.  
  264.     For the PC:
  265.  
  266.     o JPP, by Arnt Gulbrandsen (Norway).  Faster than mine (but according
  267.       to an OUTLET review slower on some boards), by using a very smart
  268.       decoding technique, but requires a 80386 or '486 processor.  Is less
  269.       compatible than Z80.  Uses the .SNA snapshot format.  Needs VGA.
  270.       Not many extra features.
  271.     o VGASPEC, by Alberto Olloqui (Spain).  Needs VGA and 80286.  Quite
  272.       slow, and crashes on quite a lot of programs.  Uses the .SP snapshot
  273.       format.  Allows ROM pokes.  This program is an illegal pre-release
  274.       of SPECTRUM, by Pedro Gimeno.
  275.     o SPECTRUM, by Pedro Gimeno (Spain).  Uses another .SP snapshot
  276.       format.  Has a tape interface.  Also quite slow.  Allows changing
  277.       the rom.
  278.     o SP, by J. Swiatek and K. Makowski (Poland).  Cannot load or save
  279.       snapshots, but can load programs using LOAD "" via a file called
  280.       TAPE_ZX.SPC.  Crashes many programs; even basic behaves weird
  281.       sometimes.  Has a built-in monitor, but no documentation.  No
  282.       border.
  283.     o SPECEM, by Kevin J. Phair (Ireland).  Also allows rom changes.
  284.       Displays the registers on screen.  Can save and load directly
  285.       from disk using LOAD/SAVE "filename" in BASIC.  Loads .PRG
  286.       snapshots, but cannot save them.  Emulates a Multiface I.
  287.     o ZX, by L. Rindt and E. Brukner (Czech Republic).  Haven't tested
  288.       its compatibility thoroughly, but one of the games supplied didn't
  289.       respond well to the keyboard, while it did work on Z80 after
  290.       conversion.  Good tape file support including headerless files,
  291.       almost identical to the multiple .TAP file mode of Z80.  Somewhat
  292.       slower than Z80.  Includes program to load from tape and convert
  293.       to tape file.  No documentation at all.
  294.  
  295.  
  296.     For the Sinclair QL:
  297.  
  298.     o SPECTATOR by Carlo Delhez, The Netherlands; shareware; supports
  299.       tape-files, Microdrives, RS232, Z80 snapshots, MBF snapshots and
  300.       Disciple (SNP) snapshots; utilities to convert Disciple, Beta and
  301.       Opus disks enclosed.
  302.     o ZM-1/2/3/4 by Ergon Development, Italy; ZM-1 is shareware, ZM-2/3/4
  303.       are commercial. They all support tape-files and Z80 snapshots, some
  304.       support Microdrives and RS232; contain a utility to transfer programs
  305.       from tape via a Spectrum to the QL.
  306.     o ZX by Andew Lavrov, CIS; shareware; supports tape-files, MBF
  307.       snapshots en Z80 snapshots; utility to read from Spectrum tapes
  308.       (and write them).
  309.  
  310.     Spectator, ZM-1 and ZX are all about as fast (approximately 30 to 40%
  311.     on a 16 MHz MC68000 machine). ZM-2/3 are faster, but this at the cost
  312.     of compatibility. ZM-4 is not an emulator, but a real-time Z80-compiler:
  313.     very fast and seems to be compatible as well.
  314.  
  315.  
  316.     For the Amiga:
  317.  
  318.     o Spectrum, by Peter McGavin.  Very good, JPP is based to a large
  319.       extent on it.  Needs about a 25MHz machine to run at full speed.
  320.       Has tape support.
  321.     o KGB.  I haven't seen it.  A bit slower than Peter's, and the
  322.       version Peter saw wouldn't work on the Amiga 3000.
  323.     o An Italian emulator which I don't know the name of.  Excellent
  324.       compatibility, rather fast.  May be shareware.
  325.     o Several unreleased emulators.  Peter knows more about them.
  326.  
  327.  
  328.     For the Atari ST/TT:
  329.  
  330.     o One, called Spectrum.  Don't know anything about it, but the doc
  331.       file is written in quite the worst English I've seen.  [This is
  332.       Arnt speaking --- I've seen worse! GAL] Available by anonymous
  333.       ftp from terminator.cc.umich.edu.
  334.     o There's another one in the make, to be released very soon as one of
  335.       the programmers told me, written by Markus Oberhumer and other(s).
  336.  
  337.  
  338.     For the Acorn Archimedes:
  339.  
  340.     o A company called Arxe wrote one, intended to be commercial but
  341.       never released because Amstrad wouldn't permit Arxe to enclose the
  342.       ROM.
  343.     o Someone called D. Lawrence wrote another, or maybe the same.
  344.       This one is floating around but nobody has any documentation.  I
  345.       don't know what its status is.  Runs at about 70% of Spectrum speed on
  346.       an ARM2, not quite perfect graphics emulation.
  347.     o There is a PC emulator for the Archimedes...
  348.  
  349.  
  350.     For the Commodore 64:
  351.  
  352.     o The Whitby Software Spectrum simulator is a rewrite of the
  353.       Spectrum Basic.  It will not run machine-code programs.  I don't
  354.       know whether it's PD, shareware, or commercial.  Quite good.
  355.       (Responds nicely to a POKE 23659,0)
  356.  
  357.     All emulators for PC, and some for the Atari, Amiga and QL are available
  358.     on the support BBS.
  359.  
  360.     There are also emulators available for the ZX81.  Carlo Delhez, who also
  361.     wrote a Spectrum emulator for the QL, wrote the ZX81 emulators XTricator
  362.     (for the QL) and XTender (for PC's).  These programs can also be
  363.     downloaded from the support BBS.
  364.  
  365.  
  366.  
  367. 1.4  Thanks
  368.  
  369.     From the very first beginning in november 1988, when I wrote the first
  370.     lines of code, Johan Muizelaar has been a very demanding and critical
  371.     user, being only satisfied when it was perfect.  And quite a few things
  372.     I would never have started working on if he hadn't insisted that I
  373.     would...
  374.  
  375.     Secondly, I have to thank Brian Gaff, who is now handling the UK
  376.     registrations for nearly a year, and besides doing lots of PR for my
  377.     program there helped me with many things, especially with the DISCiPLE
  378.     conversion program.
  379.  
  380.     Finally, I'd like to thank
  381.  
  382.     o   Lars Koeller for transforming this plain ASCII doc to LaTeX,
  383.     o   Thomas Franke for translating the entire Dutch v1.45 manual into
  384.             German,
  385.     o   Carlo Delhez for information on the '128 and several other things,
  386.     o   Andre Mostert for some more '128 info and info on EMS memory,
  387.     o   Walter Prins for many '128 programs and a nice African chat,
  388.     o   Marco Holmer for making the program such a big hit at the HCC dagen,
  389.     o   Henk de Groot, for finding and helping me work around a bug in A86
  390.             version 3.22,
  391.     o   Arnt Gulbrandsen for pointing out to me that it is not necessary to
  392.             clear a register when it contains 0,
  393.     o   Ruud Zandbergen for his digital joystick interface,
  394.     o   Ettore de Simone for finding a noisy bug, and for some very wise
  395.             remarks about theological issues,
  396.     o   Jan Garnier for providing the chips to reanimate my real Spectrum,
  397.     o   Rudy Biesma and Tonnie Stap for providing info on the DISCiPLE disk
  398.             formats,
  399.     o   Hugh McLenaghan for his very valuable help on the DISCiPLE program,
  400.     o   Burkhard Taige for various bug reports on it,
  401.     o   Ian Cull for enhancing the DISCiPLE program and fixing two early
  402.             bugs,
  403.     o   Bert Lenarts for information on the AZERTY keyboard, and
  404.     o   Andre Brus for writing the most enthousiastic letter I've ever
  405.             read!
  406.  
  407.  
  408.  
  409.  
  410. 2.  THE EMULATOR
  411.  
  412.  
  413. 2.1  Starting the emulator
  414.  
  415.     The emulator will work on any PC with at least 512K memory, with a VGA,
  416.     EGA, Hercules, CGA or Plantronics video adapter.  If available, it will
  417.     also use EMS memory, an Adlib compatible soundcard, and an analogue or
  418.     digital joystick.
  419.  
  420.     The emulator will first read in the switches that are given in the
  421.     Z80.INI file.  You can enter switches there in the same way you would on
  422.     the command line.  Lines starting with a % sign will be ignored.
  423.  
  424.     After any switches, you may specify a snapshot file on the command line.
  425.     This file will then be loaded and executed directly.  The extension .Z80
  426.     is not necessary.  The emulator will also read .SNA files (the snapshot
  427.     format of, amongst others, Arnt Gulbrandsen's JPP); you don't have to
  428.     convert them to .Z80 files (but you may want to to save disk space).
  429.  
  430.     The emulator tries to figure out what hardware is available, and uses
  431.     things as it finds it.  Most of the time this will work without you
  432.     having to tell it anything, but if you have to you can override the
  433.     defaults by putting switches on the command line.  Switches that you use
  434.     often can be put in the Z80.INI file.  If you give a switch a second
  435.     time, for instance if it is also in the Z80.INI file, it will disable it
  436.     again.
  437.  
  438.     If you are using Hercules, try starting the emulator with -xh on the
  439.     command line.  The emulator will use a non-standard Hercules mode to
  440.     display a full-screen Spectrum picture.  You may need to calibrate your
  441.     monitor to make the image steady.
  442.  
  443.     If you're using Plantronics, try -p and -q to see which gives the best
  444.     result.
  445.  
  446.     Some black-and-white VGA monitors only display one of the three RGB
  447.     colours (green most of the times), resulting in several Spectrum colours
  448.     becoming indistinguishable.  Use -xb to use grey tones instead of
  449.     colours.
  450.  
  451.     If you're using a Trident VGA with version 3 BIOS, you may see the
  452.     picture compressed at the top of the screen, while the bottom half
  453.     contains vertical white lines.  This is due to a bug in the Trident VGA
  454.     Bios.  Start the emulator with the switch -xv to get a full picture.
  455.  
  456.     If you haven't got EMS memory, the page swapping of the Spectrum 128
  457.     cannot be emulated exactly.  Most programs will work - although quite
  458.     slowly because page swapping will take much time without EMS - but some
  459.     may crash.  On 386 and 486 machines you can emulate EMS by software,
  460.     using EMM386 for instance.  Of all the EMS emulators I've tried (that's
  461.     three or four) QEMM was by far the fastest, but the EMM386 supplied with
  462.     the new DOS 6 seems to be about as fast.  A slow EMS emulator can
  463.     degrade the performance of the '128 emulation significantly!  Some
  464.     computers have hardware EMS capabilities, some '286 boards for instance.
  465.     Refer to your own documentation for details.
  466.  
  467.     And don't use hard disk based EMS emulators: the Spectrum emulator will
  468.     drive your hard disk nuts!
  469.  
  470.     There are a few Spectrum programs that have an odd stack pointer, and
  471.     run over the ram/rom boundary, for instance Deep Strike.  This crashed
  472.     version 1.45 of the emulator.  The bug has been removed in version 2: if
  473.     the emulator tries to read a word at FFFF, the processor generates an
  474.     INT 0D interrupt and the emulator will handle it correctly. However,
  475.     this won't work when an EMS emulator is installed that puts the 386 or
  476.     486 processor in virtual 8086 mode.  You can test all this by typing
  477.     CLEAR 65535:POKE 65535,0: RETURN in Basic, and the emulator will lock up
  478.     if it runs in virtual mode.  There is no simple solution to this
  479.     problem, but luckily it doesn't happen often.  If it does, the easiest
  480.     way to to solve it is to change the Spectrum program so that it uses an
  481.     even SP --- this is always possible, but not always easy!
  482.  
  483.     A very few programs (the only examples known to me are Fireman and
  484.     Thing) are quite sensitive to the relative actual execution speed of
  485.     emulated Z80 instructions, and crash if it isn't exactly right. If you
  486.     slow down the emulator, these program will run fine, because then
  487.     individual instructions are more carefully timed.
  488.  
  489.     The Spectrum 128 has a built-in sound chip.  If you have an Adlib
  490.     compatible soundcard installed, the Spectrum 128 sound will be played
  491.     through the Adlib card.  If you haven't, the loudest of the three sound
  492.     channels will be played through the internal PC speaker.  Sometimes the
  493.     effect is quite nice, sometimes it is horrible, but it's all I can do on
  494.     a standard PC.  If you don't want to have the Spectrum 128 sound played
  495.     through the internal speaker, use the switch -xi.  If you don't want the
  496.     Adlib card to be used (for instance to hear the sound through the
  497.     internal speaker) use -xa.
  498.  
  499.     If you're using the Pro-Audio Spectrum 16 sound card, do not install the
  500.     resident FM.EXE program; it causes problems with the emulator. Do make
  501.     sure that MVSOUND.SYS is installed in your CONFIG.SYS file, to make the
  502.     Pro-Audio Spectrum 16 Adlib compatible.
  503.  
  504.     The noise channels of the Spectrum 128 sound chip can work on different
  505.     frequencies, whereas the FM chips of the Adlib card cannot.  However, if
  506.     your Soundblaster is equipped with CMS chips, the noise frequency can be
  507.     programmed.  Specify -xc to use the CMS chips.  (These chips are not
  508.     available on Soundblaster Pro cards, and neither on most Soundblaster
  509.     clones).
  510.  
  511.     If you're living in Belgium or France, you are probably using an AZERTY
  512.     keyboard.  Specifying -xz on the command line will make all letter keys
  513.     and many punctuation keys work in the right way.
  514.  
  515.     If the emulator erroneously detects an analogue or digital joystick, use
  516.     the switch -kk.
  517.  
  518.     It may be annoying to have to press Num-Lock every time you use the
  519.     Spectrum 128 (because otherwise you'll have to use Shift with the cursor
  520.     keys to move the menu bar).  To make the emulator press shift by default
  521.     every time you use the PC cursor keys in '128 mode, use the switch -xs.
  522.     If you press Num-Lock now (in '128 mode), the shift-key won't be
  523.     pressed.  The 48K modes are not affected by this switch.
  524.  
  525.     The emulator can now also be run under Windows 3.1!  However, you cannot
  526.     use the tape interface, Real mode doesn't work anymore, and the keyboard
  527.     is not emulated as well as usual, because I have to scan it using BIOS
  528.     calls.  Be sure not to set the keyboard repeat rate too low; an initial
  529.     delay of 250 ms followed by 10 keys a second will do, but don't make it
  530.     slower.  Some key combinations may not work, such as ALT 4 for $.  So if
  531.     you want to use the emulator seriously then you shouldn't run it under
  532.     Windows, but it's nice to see three Spectrums run simultaneously...  If
  533.     you let the emulator run full-screen you may use EGA or VGA, if you want
  534.     to run it windowed you'll probably have to use CGA, because the virtual
  535.     video display driver of Windows cannot handle the VGA mode I use
  536.     (although it's only a standard text mode!).  You'll probably want other
  537.     default settings of some parameters (such as the video mode) if you run
  538.     the emulator under Windows; the emulator will always use the .INI file
  539.     in the directory of the Z80.EXE file so the other switches must be put
  540.     on the command line, in a .PIF file.  An example .PIF file (which runs
  541.     the emulator in windowed CGA mode) and a .ICO icon file are supplied
  542.  
  543.     Since the execution speed of a program running under Windows heavily
  544.     depends on other processes, the emulator doesn't try to measure how fast
  545.     it is running under Windows.  It says it runs at 100%, and you can slow
  546.     it down in the usual way, but the percentage is now relative to the
  547.     maximum speed, and has nothing to do with the actual execution speed.
  548.  
  549.     The emulator will automatically detect whether Windows is running, and
  550.     act appropriately.  To run the emulator in Windows compatibility mode in
  551.     a normal DOS environment, use -xw.
  552.  
  553.     When running the emulator under Desqview, use -e for EGA mode display.
  554.  
  555.     To run the emulator with a different rom than the standard one, you can
  556.     specify a rom image file on the command line.  Use the switch -xr file,
  557.     where 'file' is the name of the image file.  This file should be exactly
  558.     16384 bytes long.  It will of course not be used in Spectrum 128 or
  559.     SamRam mode.
  560.  
  561.     The emulator 'ZX' by Rindt and Bruckner comes with several roms, stored
  562.     in their tape format.  You can convert these files to .TAP files, and
  563.     then load them in the normal way, but to run the emulator with these
  564.     roms you need the bare 16K binary image file.  To extract it from the
  565.     rom files, type the following at the DOS prompt:
  566.  
  567.         C:\>debug rom.000       (or other rom file (of 16406 bytes))
  568.         -m 115 L 4000,100       (move the rom down, overwrite header)
  569.         -rcx                    (new length of exactly 16K bytes)
  570.         CX 4016
  571.         4000
  572.         -n rom000.bin           (or some other name)
  573.         -w                      (write it)
  574.         Writing 04000 bytes
  575.         -q                      (and quit)
  576.  
  577.     These are the most important switches that you have to specify when you
  578.     start the emulator.  Most of the other switches are used to select
  579.     default values for various things which can also be changed when the
  580.     emulator is started.  Some useful things to select are default
  581.     directories for .Z80, .TAP and .MDR files; these will be explained
  582.     below.
  583.  
  584.  
  585.  
  586. 2.2  Using the emulator
  587.  
  588.     When the emulator starts, you'll see the usual Spectrum copyright
  589.     message appear on screen.  Pressing F1 will pop up a small help screen
  590.     that explains the function of the function keys and various other
  591.     special keys.
  592.  
  593.     By pressing F10, you enter the main menu of the emulator.  Most of the
  594.     menu options can be chosen directly by pressing another function key.
  595.     The only exception is X, Extra functions, for which no function keys
  596.     were available anymore.  If you're somewhere deep in the menu structure
  597.     of the main menu, pressing ESC will get you one level higher most of the
  598.     time.  Pressing F10 will get you back to the main menu.
  599.  
  600.     The 'Select Hardware' menu option sits under function key F9.  There are
  601.     five configurations you can choose: a normal Spectrum 48K with or
  602.     without Interface I, a Spectrum 128K with or without Interface I, and a
  603.     Spectrum with Interface I and SamRam.  Switching to another mode will by
  604.     default reset the Spectrum.  If you don't want this to happen, press
  605.     CTRL-ENTER instead of ENTER when you've made your choice.  It cannot be
  606.     guaranteed however that the Spectrum won't crash or behave weirdly, for
  607.     obvious reasons.  Going from a Spectrum 128 to a normal Spectrum will
  608.     almost always crash it, except if you enter the SPECTRUM command before
  609.     switching.
  610.  
  611.     To use SamRam's monitor on a 128 program, switch the hardware from the
  612.     main menu, and generate an NMI (Extra functions - N) before returning to
  613.     the emulator.  This will often work, but you can't return to the program
  614.     without crashing it.
  615.  
  616.     On a real Spectrum 128, the menu bar of the startup screen is moved
  617.     using the cursor keys on the '128 keyboard.  These keys simultaneously
  618.     press a normal cursor key (5,6,7 or 8) and shift.  So you can shift the
  619.     menu bar with shift-6 and shift-7.  As is already said above, it is
  620.     possible to use the PC cursor keys for this; you have to select Cursor
  621.     joystick emulation (which is selected by default) and press Num-Lock
  622.     once to have the PC-cursor keys press the Spectrum Shift key too.  You
  623.     could also specify -xs on the command line (or put it in the Z80.INI
  624.     file) to make the PC cursor keys by default press shift for you in '128
  625.     mode.
  626.  
  627.     The Save and Load Program options (F2 and F3) will save the whole state
  628.     of the Spectrum and some of the emulators' settings to a .Z80 snapshot
  629.     file.  It will pack the data somewhat, so that the length of the file
  630.     varies.  The amount of memory saved depends on the current hardware
  631.     mode; 48K for normal Spectrum, 80K for SamRam, and 128K for Spectrum
  632.     128.  The settings that are saved are those that are program dependent,
  633.     for instance which joystick emulation is used, and more technical
  634.     settings like those of the R register, LDIR and Issue 2 emulation,
  635.     double interrupt frequency and video synchronisation.  These are
  636.     explained below.
  637.  
  638.     Loading a .Z80 file will cause several settings to be changed. Resetting
  639.     the Spectrum will not reset these settings to their default values!
  640.     Especially the joystick emulation setting change can be confusing, so
  641.     keep track of that.
  642.  
  643.     The Change Settings menu pops up if you press F4.  You can do many
  644.     things here, and I won't explain them all here.  The I and O options can
  645.     be used to redirect the RS232 output; see paragraph 2.6 for information
  646.     on this.  R - R register emulation, and L - LDIR emulation are seldom
  647.     needed.  For remarks on these options see chapter 5, and paragraph 2.8.
  648.  
  649.     2 - Issue 2 emulation will turn the emulated Spectrum in an Issue 2
  650.     Spectrum.  (This option also works, but is ridiculous, in Spectrum 128
  651.     mode).  Some very old programs (Spinads) will not respond to the
  652.     keyboard properly on Issue 3 Spectrums, and for these programs this
  653.     option was added.  Seldom needed.
  654.  
  655.     F - fast flash makes flashing go twice as fast.  Not very useful.
  656.  
  657.     S - sound enables you to turn off all sound, useful for late-night
  658.     playing.
  659.  
  660.     D - double interrupt frequency is useful for slow machines, as some
  661.     programs will run faster when this option is on.  If you're typing in a
  662.     BASIC program on a slow machine, always turn this on, since the
  663.     keyboard, which is polled by an interrupt routine, will respond much
  664.     better.  On the other hand, some programs will crash with this option
  665.     active.
  666.  
  667.     V - video synchronisation is used to remove the flickering of moving
  668.     characters in some programs.  You can choose between Normal, High and
  669.     Low.  Normal works well for almost all programs; Ghosts and Goblins and
  670.     Zynaps look much better when this is turned to High.  If you see
  671.     characters not moving smoothly or flicker, or a background not moving as
  672.     a whole, experiment a little bit with this setting, and re-save the
  673.     snapshot when you've found the best setting.  (For a slightly more
  674.     detailed discussion of this option see section 5.1)
  675.  
  676.     J - joystick emulation specifies which Spectrum joystick the PC cursor
  677.     keys (and analogue or digital joystick, if it is available) control. You
  678.     can choose from Cursor (default), Kempston, Interface 1 and 2.  As I
  679.     already said above, if Cursor joystick is chosen, the Num-Lock key
  680.     controls whether Shift is pressed too if the PC cursor keys are pressed.
  681.     (Since the shift and number keys are pressed exactly simultaneously, it
  682.     is possible that the Spectrum has already read the Shift key, but not
  683.     yet the others, when you press both keys down. Sometimes you will
  684.     therefore get the number 5,6,7 or 8 instead of a cursor movement.)
  685.  
  686.     Finally, C - Change speed lets you control the speed of the emulator. As
  687.     a side effect, slowing down the emulator makes the timing of the various
  688.     opcodes correspond more exactly to the actual timing on a real
  689.     processor.
  690.  
  691.     That concludes the discussion of the F4-'change settings' menu.  Let's
  692.     continue with the other function keys.
  693.  
  694.     F5 generates an NMI.  Only useful if in SamRam mode; otherwise it may
  695.     reset the Spectrum or (sometimes) crash a program.  ALT-F5 or CTRL-F5
  696.     resets the Spectrum.
  697.  
  698.     F6 turns on Real Mode.  Try this when the emulator is playing a tune and
  699.     sounds a little harsh.  This mode is needed when you want to load
  700.     speed-saved games from tape; see below for more information.
  701.  
  702.     F7 and F8 activate the tape- and microdrive-menus.  Again, see below for
  703.     more information.
  704.  
  705.     Resetting the Spectrum, or generating an NMI can be done from the main
  706.     menu too, in the X - Extra Functions menu.  This is useful if you want
  707.     to activate the NMI software of the SamRam for instance just after
  708.     loading a snapshot file, or just after you changed the hardware mode.
  709.     From this menu you can also shell to DOS, and save or load a screen
  710.     snapshot: a 6912 byte file with extension .SCR that contains a dump of
  711.     the screen information.  This enables you to very easily transfer
  712.     screens from one Spectrum program to another.  The .SCR files can be
  713.     converted to .GIF or .PCX files by the CONVERT program, available to
  714.     registered users.
  715.  
  716.     When you're typing BASIC-programs in 48K mode, you'll probably have to
  717.     look up some Spectrum keywords.  Further down in this documentation
  718.     there is an alphabetical list of all keywords and their key-combination.
  719.     For 'on-line' help, press ALT-F1 to see the Spectrum keyboard layout.
  720.  
  721.  
  722.  
  723. 2.3  Emulation of the Keyboard, Screen and Beeper
  724.  
  725.     The keyboard.  Letter keys are mapped to the Spectrum's letter keys. The
  726.     ALT and CTRL keys can both be used for Symbol Shift.  Then, there are a
  727.     lot of keys on the PC keyboard which don't exist on the Spectrum
  728.     keyboard.  Many of them are used, to make things easier:
  729.  
  730.     The function keys have several special functions.  See the previous
  731.     paragraph.
  732.  
  733.     CTRL-Break and CTRL-ALT-DEL quit the emulator.
  734.  
  735.     The punctuation keys - = ; ' , .  / and their shifts: _ + : " < > ? have
  736.     the effect of pressing Symbol Shift and the corresponding letter key, so
  737.     you can use these in the straightforward way.
  738.  
  739.     The ESC key presses Shift-1, EDIT, used as a sort of ESC key in many
  740.     Spectrum programs.  The Backspace key presses Shift-0, the Delete of the
  741.     Spectrum.  CapsLock presses Shift-2, Spectrum's capslock key.
  742.  
  743.     The PC-cursor keys and the numeric keypad keys 8,4,6 and 2 control the
  744.     Cursor, Interface 2 or Kempston joystick.  The TAB key, and 0,5 and
  745.     ./DEL on the numeric keypad control the fire button.  If the Cursor
  746.     joystick is selected, you can select whether Shift should also be
  747.     pressed with the NumLock key (but see the discussion above of the -xs
  748.     switch).
  749.  
  750.     If you're running the emulator on a slow computer, try selecting double
  751.     interrupt frequency.  Most programs poll the keyboard by interrupt, in
  752.     any case the ROM does, and doubling the frequency with which this
  753.     happens will make the emulated Spectrum react much more quickly on your
  754.     keystrokes.
  755.  
  756.     If you've got an AZERTY keyboard, the standard mappings of the keys
  757.     won't work at all properly.  Include the switch -xz in your Z80.INI file
  758.     in this case; many punctuation keys will now also work properly. There
  759.     is no support for other non-US keyboard layouts; sorry!
  760.  
  761.  
  762.     Now about the screen emulation.  Fifty times an (emulated) second, the
  763.     screen is checked for changes.  If anything has changed, the change is
  764.     displayed on the PC screen.  It turned out that this was the fastest
  765.     method of updating the screen.
  766.  
  767.     I tried to update the screen at about the same time the real Spectrum
  768.     shows it on the TV screen, relative to the 50 Hz interrupt.  There is a
  769.     problem; the Spectrum takes about 1/100th of a second to generate the
  770.     whole picture, while I stop the emulator at some point in the 1/50th-
  771.     of-a-second cycle and display the whole screen at once.  Usually this
  772.     makes little difference, but with some programs it does: characters may
  773.     flicker heavily or disappear entirely (see for instance BC's Quest for
  774.     Tires).  By selecting the 'video synchronisation mode', you have some
  775.     control over the exact point of the cycle at which the screen is
  776.     updated.
  777.  
  778.     In the Hercules, CGA and Plantronics modes, not all colours can be
  779.     displayed.  In the EGA mode, all colours can be displayed, but some
  780.     colours have the same intensity in bright 1 as in bright 0.  In VGA
  781.     mode, all colours closely resemble the original Spectrum colours, and
  782.     furthermore in this mode the screen updating is the fastest of all
  783.     modes.
  784.  
  785.     The border updated every 1/50th of a second, so you cannot see the
  786.     familiar stripes when saving.  However, in real mode the emulator uses
  787.     the overscan of EGA to display the border, and you can see some stripes
  788.     there, and in VGA mode the border can be shown full-size.  The only
  789.     drawback of the border emulation in real mode is that there appears some
  790.     'snow' on the screen at each OUT - I don't know a way around this.
  791.  
  792.  
  793.     Finally, the sound emulation.  The Spectrum beeper is emulated by the
  794.     PC's internal beeper.  Because every 1/50th of a second the screen has
  795.     to be updated, and this takes a little time even if there are no
  796.     changes, the sound is a bit harsh.  If you select real mode, the
  797.     emulator won't update the screen anymore and the sound will sound
  798.     better.
  799.  
  800.     The sound of the Spectrum 128's sound chip is played through the Adlib
  801.     card; if you haven't got such a card some notes are played through the
  802.     internal speaker.  That sound will be turned off, however, as soon as
  803.     the program makes a sound through the normal speaker of the Spectrum.
  804.     Some Spectrum 128 programs use the sound chip and the beeper at the same
  805.     time, and this won't work properly without an Adlib card.
  806.  
  807.  
  808.  
  809. 2.4  Using the tape
  810.  
  811.     This emulator can load programs that are saved to tape in the usual way,
  812.     but also speed-saved programs can be loaded.  Furthermore, you can also
  813.     make a disk file act as an 'emulated tape', so that the normal SAVE and
  814.     LOAD commands can be used to transfer data to and from disk easily.
  815.  
  816.     Let's first discuss the saving and loading of programs using a tape
  817.     recorder - that'll be the first thing you want to do, to transfer your
  818.     programs to the PC.  First of all, you need an interface to connect the
  819.     tape recorder to the PC.  The parallel printer interface is used for
  820.     this.  All you need is a very simple and cheap piece of electronics to
  821.     get the input and output signals at the appropriate and safe levels; the
  822.     circuit diagram is in the program DIAGRAM.Z80.  The interface has to be
  823.     calibrated; a program to help doing this is contained in the snapshot
  824.     file.  Adjust the variable resistor so that when the tape recorder is
  825.     played at normal volume, the bar points just below 50%. When the tape
  826.     recorder is turned off, the bar should go to 0%.
  827.  
  828.     You have to tell the emulator which LPT port you use for tape I/O.  This
  829.     can be selected in the tape menu, but it can also be specified on the
  830.     command line or in the Z80.INI file with the -b switch; for instance
  831.     -b2 selects LPT2.  Default is LPT1.
  832.  
  833.     There are two ways to load programs: in 'real' or normal mode.  In real
  834.     mode, the emulator doesn't update the screen or scan the keyboard
  835.     anymore, so that the emulated Spectrum program can run smoothly.  The
  836.     emulator has to run at about 100%, but then you're able to load
  837.     everything a normal Spectrum would load, including speed-saved programs.
  838.     The only thing you see on screen are the loading bars in the border (on
  839.     EGA or VGA screens).  Real mode is selected by pressing F6. Saving
  840.     programs in real mode is a bit useless but it works; enter the SAVE
  841.     command, press a key to start saving and quickly press F6 when the
  842.     saving starts.  It will continue in real mode.
  843.  
  844.     If your computer is just fast enough, don't slow the emulator down too
  845.     much.  Because the IN instruction is relatively slow, the emulator has
  846.     to run at about 110% for the best results.  If your computer is really
  847.     fast, you can best slow it down to exactly 100%.  If your computer is
  848.     just a bit too slow, you can try to make your tape recorder run slower
  849.     too (usually you can do this by adjusting a little screw at the back of
  850.     the motor), I successfully loaded several speed-saved programs at 90%.
  851.  
  852.     In normal mode, the standard ROM loading and saving routines are
  853.     'trapped' (at addresses 04d8 and 056a) when they're about to start
  854.     saving or loading.  A routine in the emulator itself then takes over,
  855.     and loads or saves a block to tape or a disk file.  By default, this
  856.     routine uses the tape instead of a file, and I'll discuss that mode of
  857.     operation first.
  858.  
  859.     Using these SAVE and LOAD routines has a great advantage as well as a
  860.     disadvantage compared to using the Spectrum's own routines in real mode.
  861.     The advantage is that the internal routines work on every machine, no
  862.     matter how slow or fast, without having to make the emulator run at
  863.     100%.  The disadvantage at using them is that they obviously won't
  864.     understand speed-saved files.  For normal use, these internal routines
  865.     work much easier, and real mode loading is only necessary for
  866.     speed-saved and very well protected programs.
  867.  
  868.     So far for the general information about tape loading.
  869.  
  870.     The emulator uses files with the extension .TAP to hold a piece of
  871.     'tape', with several blocks on it.  Each block is usually either a
  872.     header or a data block; a normal file thus consists of two blocks. There
  873.     are two modes of operation when loading and saving to disk files, single
  874.     and multiple .TAP file mode.
  875.  
  876.     In single .TAP file mode, each block saved is appended to the end of the
  877.     .TAP file, like would happen if you were actually saving to tape. In the
  878.     same way, when loading in single file mode, each time the ROM wants to
  879.     load a block, it is presented the next block in the .TAP file. It is
  880.     handled as it would if the block was loaded from tape, that is, if the
  881.     ROM needs a header and is presented a data block, it will skip it.  The
  882.     header will however be considered to be read.  So, entering LOAD
  883.     "rubbish" will show all headers in the .TAP file, just as an ordinary
  884.     Spectrum would show all headers on the tape if you left the tape
  885.     running.
  886.  
  887.     If the last block is loaded, the file pointer is moved to the start
  888.     again.  So a .TAP file can be considered to be an infinite tape. Single
  889.     .TAP file mode is useful to save whole programs to disk, or for
  890.     multi-load games that need to load in levels as you play.
  891.  
  892.     A sort of 'random access' file management would also be useful, for
  893.     instance when you're developing a program and need to save several
  894.     pieces of data to disk and later load back a specific one.  This can be
  895.     done in single .TAP file mode (by positioning the file pointer using the
  896.     Browse function), but there's a different mode of operation that makes
  897.     things easier: multiple .TAP file mode.  In fact, by default the
  898.     emulator is in this mode.
  899.  
  900.     When the emulator is in multiple .TAP file mode, it will read all blocks
  901.     from all .TAP files in a specified directory, one after the other.  When
  902.     it has finished reading the last one, it will start all over again.
  903.  
  904.     When saving, the emulator will put the two blocks of a normal file, the
  905.     header and the data block, in one .TAP file with a unique name made up
  906.     of the printable letters of the file name and a two-digit number.  The
  907.     name of the .TAP file is irrelevant to the emulator, but to have it
  908.     resemble the name of the actual Spectrum file you saved is simply
  909.     convenient.  If the Spectrum program saves a data block to tape without
  910.     first saving a header, the .TAP file will contain only this data block,
  911.     and the DOS file name will be HDRLES, with a two-ditit number appended
  912.     to make it unique.  The format of the .TAP files saved in multiple .TAP
  913.     file mode is exactly the same as the format used in single .TAP file
  914.     mode.
  915.  
  916.     You can easily string together .TAP files; for instance a number of .TAP
  917.     files created in multiple .TAP file mode can be put into one big .TAP
  918.     file simply by copying them together, e.g.
  919.  
  920.         COPY /B FILE1.TAP + FILE2.TAP ALL.TAP.
  921.  
  922.     (Note: in some versions of DR DOS the /B switch, necessary because
  923.     otherwise copying stops after a CTRL-Z character, doesn't work properly;
  924.     load your old COMMAND.COM to copy the files).
  925.  
  926.     Now you know what you can do, but how to get the emulator to do it?
  927.     That's what the final section is about: the tape menu.
  928.  
  929.     Press F7 to enter the tape menu.  Pressing S will select or de-select
  930.     single file mode.  By default, multiple .TAP file mode is selected.  In
  931.     this case, there are three other possible choices in this menu.  First
  932.     of all, D selects a tape-file directory where the .TAP files will be
  933.     saved into and loaded from.  A default directory can be selected by
  934.     putting the -xs switch on the command line or in the Z80.INI file; for
  935.     example -xs c:\spectrum\taps selects that directory.
  936.  
  937.     The I and O options are used to select the source and destination of the
  938.     saving and loading: the LPT port for a physical tape recorder, or 'disk'
  939.     for disk files.  By default LPT1 is selected; another LPT port can be
  940.     selected with for instance -b2 or by pressing I and O.  Input and output
  941.     are directed to disk by default if a default tape file directory is
  942.     given by means of a switch on the command line or .INI file.
  943.  
  944.     If Single .TAP file mode is selected, different and more menu options
  945.     appear.  With G and P, the input and output tape files can be selected.
  946.     They may be the same.  If a specified output file already exists, you
  947.     may choose to append to or overwrite this old file.  Saving is always at
  948.     the end of the file; loading always starts at the beginning of the .TAP
  949.     file.
  950.  
  951.     With the B option - Browse - the position of the file pointer into the
  952.     input .TAP file can be changed.  If you, for instance, type LOAD""
  953.     instead of LOAD "" CODE, the first header is read, and you would have to
  954.     read all other headers before trying to load the file again.  With the
  955.     browse option you can conveniently change the file pointer.  Of every
  956.     header (that is, every block with flag byte 0 and length exactly 17) the
  957.     name and type, and of every data block the length is shown.
  958.  
  959.     The option B can also be used to delete specific blocks from a .TAP
  960.     file.  Make sure you do not only delete a data block or a header, or the
  961.     ROM may get confused! (Double data blocks will be skipped, but double
  962.     headers can generate Tape Loading errors).
  963.  
  964.     As in multiple .TAP file mode, I and O are used to specify the source
  965.     and destination for saving and loading.  If you enter a .TAP file name
  966.     with G or P, this will automatically be set correctly.  You can then
  967.     always reset the input or output back to LPTn again, of course.
  968.  
  969.     Finally, in Single .TAP file mode you can use 'tape mirroring': loading
  970.     programs from tape (in normal mode, i.e.  not using Real mode) and at
  971.     the same time saving a copy of each block loaded into a .TAP file.  This
  972.     .TAP file can later be used to load the program again, might anything go
  973.     wrong.  There are two ways of mirroring: normal mirroring and exact
  974.     mirroring.  The last one must be used only in exceptional cases; it will
  975.     always make a copy of a block, even if it had a tape error (the
  976.     corresponding block in the .TAP file will also have a tape error).  This
  977.     causes ticks in leader tones to make 0-byte blocks, so the .TAP file may
  978.     get messy.  Do not use exact mirroring if you don't really have to; I
  979.     think normal mirroring will always work in practice.
  980.  
  981.     If you try to leave the tape menu when for instance tape mirroring is
  982.     selected, and no output filename is given, the emulator will warn you
  983.     and will insist that the error be corrected.  Yes, it's stubborn!
  984.  
  985.  
  986.  
  987. 2.5  Using the microdrive
  988.  
  989.     Compared to the tape, this is really simple.  Cartridges are emulated by
  990.     files of 137923 bytes.  These files have the extension .MDR, and can
  991.     contain up to 126K of data.  The emulator emulates 8 microdrives, the
  992.     maximum amount the Interface I software can handle, and each of these
  993.     cartridge files can be inserted in any of the 8 microdrives.  (Do not
  994.     insert one file into more than one microdrive; this will cause problems
  995.     with the buffering done by the emulator as well as the Interface I, and
  996.     might result in data loss).
  997.  
  998.     Press F8 to enter the microdrive menu.  Press 1 to 8 to select a
  999.     microdrive, and I to insert a microdrive cartridge.  You can select an
  1000.     existing one, or type a new name.  If the cartridge file isn't found,
  1001.     the emulator asks whether it should create it.  When created, you'll
  1002.     have to format it first; if you don't, you'll get a 'microdrive not
  1003.     present' error when you try to read it, just as happens with real
  1004.     unformatted cartridges.  To format a cartridge, type
  1005.  
  1006.         FORMAT "m";1;"name"
  1007.  
  1008.     After this the cartridge should have 126K of free space.
  1009.  
  1010.     The cartridge can be write protected; see the menu option in the F8
  1011.     menu.  This is a characteristic of the cartridge, and the write protect
  1012.     tab information is therefore stored in the cartridge file.
  1013.  
  1014.     As on the real Spectrum, you'll have to be careful with OUT's if a
  1015.     cartridge is inserted.  Try OUT 239,0 (on a real Spectrum, this turns on
  1016.     the microdrive motor) and wait a few seconds; most of your data will be
  1017.     lost!  You can stop the microdrive motor by typing STOP (or, more
  1018.     generally, generate an error).
  1019.  
  1020.     The microdrives are emulated at IN/OUT level.  This means that every
  1021.     utility or program that uses microdrives ought to work on the emulator.
  1022.     Most utilities use hook codes, and these will certainly work.
  1023.  
  1024.     The GAP line is emulated; this signal is activated if the interface I
  1025.     senses a piece of tape with no data on it.  If the checksum of the first
  1026.     header block of a microdrive header or data block is not correct, that
  1027.     block is considered to be a GAP.  This will only happen if some utility
  1028.     writes a bad block to microdrive deliberately, if the file is newly
  1029.     created and unformatted, or when you type OUT 239,0.
  1030.  
  1031.  
  1032.  
  1033. 2.6  Using the RS232 channel
  1034.  
  1035.     This was the only Spectrum i/o channel that could be used in the early
  1036.     versions of the emulator.  Using .TAP files instead of the RS232 channel
  1037.     is often easier, but sometimes using the RS232 channel can be very
  1038.     useful too, for instance if you've got a null-modem lead that connects a
  1039.     Spectrum with interface I to the PC you can use it to transfer data and
  1040.     programs easily.  Furthermore, the RS232 channel is the easiest way to
  1041.     let the emulator communicate with a PC printer.
  1042.  
  1043.     The Interface I RS232 port is called the "B" or "T" channel.  The first
  1044.     is the binary channel, the "T" channel won't let all control codes
  1045.     through and will expand any keyword; useful for LISTing a program but
  1046.     otherwise annoying.
  1047.  
  1048.     The Spectrum 128 has its own RS232 port; it is called the "P" channel.
  1049.     Output to either the Interface I's or Spectrum 128's own RS232 port will
  1050.     all be processed as 'RS232 output', and input will go to both (that is,
  1051.     to the one you happen to read from).
  1052.  
  1053.     The output to the RS232 channel can be routed to an LPT port, to a COM
  1054.     port or to a file on disk.  Input can come from either a file or a COM
  1055.     port.
  1056.  
  1057.     If you want to use the RS232 channel for printing using LPRINT and LLIST
  1058.     (shorthand for PRINT #3 and LIST #3), be sure to open that channel for
  1059.     output to RS232; by default it sends its output to the ZX Printer, which
  1060.     is not supported.  You can open the channel by typing OPEN #3,"B" (or
  1061.     "T" for listings, or "P" on a Spectrum 128).
  1062.  
  1063.     Input and output are buffered.  This is important to remember when
  1064.     you're transferring files using the SAVE and LOAD *"b" commands of the
  1065.     Interface I.  If the header is missed, for instance if you try to load
  1066.     the wrong file type, re-sending the file will not directly work because
  1067.     there will still be bytes in the buffer.  You have to clear the input
  1068.     buffer before re-sending the file.  When inputting from a disk file, the
  1069.     file pointer can be reset to point to the start of the file again to
  1070.     re-read the header.
  1071.  
  1072.     When inputting or outputting from or to a disk file, the read or write
  1073.     position is displayed as a byte-count.  An <EOF> sign will appear if an
  1074.     input file is read completely through to the end.
  1075.  
  1076.     The RS232 redirection options are in the Change Settings (F4) menu.  The
  1077.     menu options are pretty obvious if you keep above remarks in mind, so I
  1078.     won't go into that.
  1079.  
  1080.     When using a COM port, make sure you have initialised it before starting
  1081.     the emulator with the Dos MODE command, for instance
  1082.  
  1083.         MODE com1:96,n,8,1
  1084.  
  1085.     initialises COM1 to send and receive at 9600 baud, no parity, 8 data
  1086.     bits and 1 stop bit, the default for the Interface I.
  1087.  
  1088.     Here is how to transfer programs from a Spectrum to the PC using the
  1089.     RS232 lead.  First, you need a null-modem lead.  I myself use the
  1090.     following cable:
  1091.  
  1092.           Spectrum                                  'AT'      'PC'
  1093.           (9 pins)                                (9 pins)  (25 pins)
  1094.  
  1095.             3 TxD   ───────────────────────── RxD     2         3
  1096.  
  1097.             4 DSR   ───────────────────────── DTR     4        20
  1098.  
  1099.                                         ┌──── CTS     7         4
  1100.                                         └──── RTS     8         5
  1101.  
  1102.             7 GND   ───────────────────────── GND     5         7
  1103.  
  1104.     (so CTS and RTS have to be connected!) This is not a full null-modem
  1105.     lead; you can only send data from the Spectrum to a PC.  Here's how to
  1106.     transfer: load the program SAVESPEC.Z80 in the emulator and type the
  1107.     basic program over into the real Spectrum, and run it.  It saves a short
  1108.     piece of code to tape.
  1109.  
  1110.     Now load the program you want to transfer, and stop it.  (This may be
  1111.     tricky!)  Load the code back into memory at address 16384 (the code is
  1112.     relocatable but this is the safest place):
  1113.  
  1114.         LOAD "RS232" CODE 16384
  1115.  
  1116.     Now open channel three for output to RS232; on a Spectrum with Interface
  1117.     I this would be OPEN #3,"b", on a Spectrum 128 it would be OPEN #3,"p",
  1118.     and with other interfaces you'll probably know what to do. Select the
  1119.     right baud rate on the Spectrum (probably FORMAT "b",9600 or something
  1120.     like that).  Now initialise the appropriate COM port on the PC and type
  1121.  
  1122.         GETRS /n filename.z80           (n=COM port used)
  1123.  
  1124.     at the DOS prompt, and then type RANDOMIZE USR 16384 to send the whole
  1125.     memory over to the PC.  The resulting .Z80 file should now be exactly
  1126.     49182 bytes long (that is 48K+30 bytes), if not try again or try a lower
  1127.     baud-rate.  Voila, transferred!
  1128.  
  1129.     To transfer short blocks of data it's often easier to use the LOAD *"b"
  1130.     and SAVE *"b" commands of the Interface I.  When the right options have
  1131.     been selected in the RS232 i/o redirection menu, you should just follow
  1132.     the instructions of the Interface I user manual and all should work as
  1133.     expected.
  1134.  
  1135.  
  1136.  
  1137. 2.7  Joysticks
  1138.  
  1139.     As was already said in the introduction, the emulated Spectrum joystick
  1140.     (Cursor, Interface 2 or Kempston) is controlled by the PC cursor keys
  1141.     and 5/0/.  on the numeric keypad and TAB as fire keys.  The emulated
  1142.     joystick can also be controlled by a real joystick, both an analogue (PC
  1143.     standard) or a digital one.
  1144.  
  1145.     The analogue joystick support is rather straightforward.  If you've got
  1146.     one, it works - it couldn't be simpler.  The digital joystick support is
  1147.     less obvious, since PC's don't support these.
  1148.  
  1149.     To use digital joysticks, Ruud Zandbergen has made a device that uses
  1150.     the two inputs of a normal analogue joystickinterface to connect a
  1151.     digital joystick to a PC.  Here's the circuit diagram:
  1152.  
  1153.  
  1154.     15 pins male  (pc)                           9 pins male (joystick)
  1155.  
  1156.  
  1157.     1+9 <─────────┬───────────┬────────────┬─────────────┬──> 7 (5V)
  1158.                  ┌┴┐         ┌┴┐          ┌┴┐           ┌┴┐
  1159.         4 x 1 kΩ │ │ ¼ Watt  │ │          │ │           │ │
  1160.                  └┬┘         └┬┘          └┬┘           └┬┘
  1161.     3   <─────────┴──> 4 (up) │            │             │
  1162.                               │            │             │
  1163.     6   <─────────────────────┴──> 3 (dwn) │             │
  1164.                                            │             │
  1165.     13  <──────────────────────────────────┴──> 1 (rght) │
  1166.                                                          │
  1167.     11  <────────────────────────────────────────────────┴──> 2 (lft)
  1168.                        ┌────────────────────┐
  1169.     2   <──────────────┤    47 Ω ¼ Watt     ├───────────────> 6 (fire)
  1170.                        └────────────────────┘
  1171.     4+5+14 <────────────────────────────────────────────────> 8 (0V)
  1172.  
  1173.  
  1174.     4+5+14 means: connect pins 4, 5 and 14.  The same applies for pins 1 and
  1175.     9.  Here's the list of ingredients:
  1176.  
  1177.         1 x 9 pins D plug, male
  1178.         1 x 15 pins D plug, male
  1179.         4 x 1kΩ , ¼ Watt resistors
  1180.         1 x 47 Ω, ¼ Watt resistor
  1181.         piece of 7-wire flatcable
  1182.  
  1183.     Everything can be fit into the 15-pins plug.  Make sure the resistors
  1184.     don't touch the other blank connections!  This interface can be used for
  1185.     all usual digital joysticks, with or without auto fire (that is every
  1186.     joystick that work with a Kempston joystick interface, or that work on a
  1187.     Commodore 64/Amiga or Atari).  The joysticks for the Spectrum +2/+3 will
  1188.     not work, however the pin layout is easy to change.
  1189.  
  1190.     This joystickinterface needs an analogue PC-joystickinterface on which
  1191.     you can connect TWO analogue joysticks (on one plug!).  Most cards can
  1192.     do this, but some multi-I/O cards support only one joystick.  Check the
  1193.     documentation of your I/O card to see whether your joystickinterface is
  1194.     suitable.  The soundblaster joystick interface works fine.
  1195.  
  1196.     A number of PC games will behave strange when the digital joystick
  1197.     interface is connected; they run very slow or crash.  When this happens,
  1198.     remove the joystick interface (not only the joystick!).
  1199.  
  1200.  
  1201.  
  1202. 2.8  Transferring programs
  1203.  
  1204.     There are a number of ways to transfer programs from the Spectrum to the
  1205.     PC: loading them directly from tape, using the RS232 lead or
  1206.     transferring from disks of Spectrum disk interfaces.  And then you might
  1207.     have snapshot files from other emulators that you want to convert to
  1208.     .Z80 files.  I'll discuss these cases one after the other.
  1209.  
  1210.     Converting using the COM port is not so easy most of the times, but if
  1211.     you've got a null-modem lead waiting to do something you could read
  1212.     section 2.6.  Luckily, there are easier ways.
  1213.  
  1214.     First of all, you can use the tape.  If you want to do this, then the
  1215.     first thing to do is to read section 2.4 carefully - now you know almost
  1216.     everything you need.  Most programs you have probably use the normal
  1217.     tape format; you will find that these usually load right away. If the
  1218.     programs use speed-load, using real mode will probably load most of
  1219.     these right away too.
  1220.  
  1221.     But some programs are really cleverly protected, and use obscure
  1222.     features of the Z80 processor.  To run these programs, turn on LDIR
  1223.     emulator and R register emulation (see the 'Change Settings' menu, F4).
  1224.     Note that the emulator will slow down a bit when R register emulation is
  1225.     selected; if you need to use real mode then make sure you speed the
  1226.     emulator up again to 100%.  After the program has loaded successfully,
  1227.     you may try to turn R register emulation off again; I don't know any
  1228.     program that needs R register emulation after loading.  Read chapter 5
  1229.     for more technical information about these options.
  1230.  
  1231.     If you've got Spectrum disks, you will probably be able to convert the
  1232.     programs on them to a useful format and use them in the emulator.  The
  1233.     registered package of this emulator contains a program DISCIPLE, that
  1234.     can read DISCiPLE and Plus D disks and convert the snapshots and other
  1235.     files on it to .TAP and .Z80 files.  The previous version of this
  1236.     program could only read 3.5'' Disciple disks, and had several bugs in
  1237.     the file and snapshot translation routines.  So if you transferred
  1238.     programs with the old DISCIPLE program and they don't work, don't blame
  1239.     the emulator but try to transfer them again with the new program.
  1240.  
  1241.     The current version of the DISCIPLE program reads 3.5'' as well as
  1242.     5.25'' DISCiPLE disks, will translate 48K, 128K and screen snapshots,
  1243.     and other normal files.  The previous version used the .SAV file format
  1244.     for normal files, which could be loaded into the emulator using LOAD
  1245.     *"b"; this version converts them into .TAP files which can be loaded
  1246.     simply by using the normal tape LOAD statements (see 2.4).
  1247.  
  1248.     The DISCiPLE interface modifies the Spectrum system variables in such a
  1249.     way that LPRINT sends its output to DISCiPLE's own printer interface.
  1250.     When you transfer a snapshot that uses the printer, you'll have to tell
  1251.     it to use the Interface I's RS232 printer output instead, by breaking
  1252.     the program and typing OPEN #3,"b".  If you don't, you'll get strange
  1253.     results.
  1254.  
  1255.     If you have got a Beta disk interface, your problem is solved too. J.L.
  1256.     Bezemer wrote a program called BDDE that reads Beta disks.  The program
  1257.     can be downloaded from the Spectrum emulator support BBS.
  1258.  
  1259.     Finally, maybe you were using another Spectrum emulator for the PC
  1260.     before using this one, and you may have already got a collection of
  1261.     snapshot or other files.  CONVZ80, another utility for registered users,
  1262.     can convert between several snapshot formats, namely the .SNA format of
  1263.     JPP, the .SP formats of VGASPEC and SPECTRUM, the .PRG files of SpecEm,
  1264.     and the .Z80 format of course.  (It is by the way not necessary to
  1265.     convert .SNA files, the emulator can read them as they are.)  CONVZ80
  1266.     can also convert the tape files used by SpecEm and ZX to .TAP files.
  1267.     CONVZ80 recognizes what it should do by the extension of the files you
  1268.     enter on the command line; to distinguish between VGASPEC's and
  1269.     SPECTRUM's .SP formats you can use the switch -o.  If the extension
  1270.     consists of digits only, it is taken to be a ZX tape file, and if it
  1271.     contains non-digits and is none of .SP, .Z80, .SNA, .PRG or .TAP it is
  1272.     regarded as a SpecEm tape file.
  1273.  
  1274.     SpecEm can load .PRG snapshot files, but cannot save them.  However, it
  1275.     emulates the Multiface I, which can save snapshots to tape.  SpecEm will
  1276.     save these blocks as tape files to disk.  If you convert these to a .TAP
  1277.     file (in the correct order!), you can load them into Z80 and save the
  1278.     program as a .Z80 file.
  1279.  
  1280.  
  1281.  
  1282. 2.9  Converting file formats - the utility CONVERT
  1283.  
  1284.     This section is about the utility CONVERT, which can convert some of the
  1285.     Spectrum's own format into each other, and also converts some of the
  1286.     emulator's formats into others.  It is not about converting files from
  1287.     other emulators; read section 2.8 if you want to know about that.
  1288.  
  1289.     CONVERT was useful when the emulator could only communicate with
  1290.     snapshot files and the RS232 link.  It has become less useful now, with
  1291.     .TAP files, but it still has some useful features.
  1292.  
  1293.     It can read three types of input files: pure ASCII, pure bytes (for
  1294.     instance a .SCR screen dump), and files produced by a SAVE *"b" command.
  1295.  
  1296.     Output is pure bytes, ASCII with either CR (Spectrum standard) or CR/LF
  1297.     (PC standard) for line breaks, SAVE *"b" files containing a Basic or
  1298.     code file, a .PCX or a .GIF file.
  1299.  
  1300.     So what can you do? Main uses are adding LF (10 hex) bytes to a text
  1301.     file produced by the Spectrum; converting a code block into a SAVE *"b"
  1302.     to load it into the Spectrum using LOAD *"b" (and the reverse of course:
  1303.     converting a SAVE *"b" file to pure bytes), and converting a screen dump
  1304.     to .PCX or .GIF graphics files.
  1305.  
  1306.     Less useful, but possible: LISTing a program (SAVE *"b" file) to produce
  1307.     readable ASCII, and the reverse: converting an ASCII listing to
  1308.     executable Basic again.
  1309.  
  1310.     If you want to make a .PCX or a .GIF file, input should be a SAVE *"b"
  1311.     file of a screen (length 6921 bytes exactly) or a bare .SCR screendump
  1312.     (length 6912 bytes).  You can make screendumps by selecting the X-Extra
  1313.     functions menu from the main menu.
  1314.  
  1315.  
  1316.  
  1317. 2.10  The utilities Z802TAP and TAP2TAPE
  1318.  
  1319.     The SamRam has built in it some snapshot software.  Using this software
  1320.     you can save any 48K Spectrum program to tape or to a .TAP file, as is
  1321.     explained in section 3.2 below.  But the SamRam software cannot handle a
  1322.     128K program.
  1323.  
  1324.     The utility that can convert a 128K snapshot (and 48K ones for that
  1325.     matter) to a .TAP file is called Z802TAP.  The .TAP file includes a
  1326.     basic loader, and a loading screen if you want.  Z802TAP compresses the
  1327.     blocks it writes (using a better method than used in compressing .Z80
  1328.     files) to save loading time.  If you don't want it to compress the
  1329.     blocks, for instance when you want to take a look at the ram pages of
  1330.     the Spectrum 128, specify -u when you run Z802TAP.  You can load the
  1331.     converted program simply by executing
  1332.  
  1333.         Z80 -ti tapefile
  1334.  
  1335.     and typing LOAD "" (for a 48K program) or changing the hardware mode to
  1336.     Spectrum 128 and choose 'Tape Loader' in the menu.
  1337.  
  1338.     The program TAP2TAPE writes .TAP files back to tape.  The program
  1339.     consists of a batch file TAP2TAPE.BAT, which executes the TAP2TAPE.Z80
  1340.     file using the emulator.  The .TAP file is written to tape exactly as it
  1341.     is, so that if a block contains a tape error, it won't load correctly
  1342.     from tape either.  If the entire .TAP file has been saved the emulator
  1343.     will start loading from tape.  At that point, press space once to return
  1344.     to DOS.
  1345.  
  1346.  
  1347.  
  1348.  
  1349. 3.  THE SAMRAM
  1350.  
  1351.  
  1352. 3.1  Basic extensions
  1353.  
  1354.     The SamRam is a hardware device Johan and I built for our Spectrums.  It
  1355.     consists of a 32K static RAM chip which contains a modified copy of the
  1356.     normal Basic ROM and a number of other useful routines, like a monitor
  1357.     and snapshot software.  You can compare it to a Multiface I interface,
  1358.     but it's more versatile.  Another useful feature was a simple hardware
  1359.     switch which allowed use of the shadow 32K Ram, present at 8000-FFFF in
  1360.     most Spectrums, but hardly ever actually used.
  1361.  
  1362.     For more details on the low-level hardware features of the SamRam read
  1363.     chapter 5.  In this chapter I'll explain the software features of the
  1364.     SamRam software, somewhat bombastically called the 'SamRam 32 Software
  1365.     System' or the 'Sam Operating System'.  By the way, all similarity
  1366.     between existing computers is in fact purely coincidental and has in no
  1367.     way been intended.  Really!
  1368.  
  1369.     The SamRam offers a few new Basic commands, and a lot of useful routines
  1370.     that are activated by an NMI, i.e.  by pressing F5.  First I'll discuss
  1371.     the Basic extension.
  1372.  
  1373.     Select the SamRam by starting the emulator with the -s switch, or by
  1374.     selecting it from the F9 menu.  Normal Basic functions as usual; the
  1375.     character set is different from the original one.  There are four new
  1376.     commands: *RS, *MOVE, *SAVE and *SPECTRUM, and two new functions, DEC
  1377.     and HEX, which have replaced ASN and ACS.  DEC takes a string argument
  1378.     containing a hexadecimal number, and returns the decimal value of it.
  1379.     HEX is the inverse of the DEC function, and yields a four-character
  1380.     string.
  1381.  
  1382.     *RS sends its arguments directly to the RS232 channel.  You don't have
  1383.     to open a "b" or "t" channel first.  You're right, it's of limited use.
  1384.     Example: *RS 13,10
  1385.  
  1386.     *MOVE is useful: it moves a block of memory to another place.  Example:
  1387.     *MOVE 50000,16384,6912 moves a screen-sized block from 50000 to the
  1388.     start of the screen memory.
  1389.  
  1390.     *SAVE works like *MOVE, except that it activates the shadow SamRam ROM
  1391.     before moving.  I used this command to update the shadow ROM, but on the
  1392.     emulator you can use it to move the shadow ROM to a convenient place in
  1393.     Ram where you can take a look at it, for instance by executing
  1394.     *SAVE 0,32768,16384.
  1395.  
  1396.     *SPECTRUM resets the SamRam Spectrum to a normal one.  You lose all data
  1397.     in memory.  By resetting the emulator by pressing ALT-F5, the SamRam is
  1398.     activated again.  Not very useful either.
  1399.  
  1400.     Then there's the Ramdisk, which is, like the Spectrum 128 ramdisk,
  1401.     accessed via the SAVE!, LOAD!, CAT!, ERASE! and FORMAT!.  The syntax is
  1402.     straightforward.  FORMAT! and CAT! need no parameters; ERASE! only needs
  1403.     a name.  If a file is not found, the SamRam will respond with a 5-End of
  1404.     File error.  The Ramdisk has a capacity of 25K.
  1405.  
  1406.  
  1407.  
  1408. 3.2  The NMI software
  1409.  
  1410.     Select the SamRam (F9-3), and press F5.  A menu with eight icons pops
  1411.     up.  You can select each icon by moving the arrow to it (using the
  1412.     cursor keys or the Kempston joystick), and pressing '0' or fire.  The
  1413.     icons can also be selected by pressing the appropriate letter key.
  1414.  
  1415.     The eight icons are two arrows with N and E within them, a magnifying
  1416.     glass with the letters 'mc' in it (activated by pressing D), two screens
  1417.     (identified by 1 and 2), a printer (P), a cassette (S) and a box saying
  1418.     'overig'.  The 'D' activates the monitor or disassembler; read section
  1419.     3.3 for information on this program.
  1420.  
  1421.     Pressing N or E returns you to the Spectrum.  If you pressed N, the
  1422.     normal Spectrum rom will be selected when the NMI software returns; if
  1423.     you press E, the Rom with the Basic extensions will be selected.  Some
  1424.     games may crash if they see a different rom than the standard Spectrum
  1425.     one.
  1426.  
  1427.     Pressing 1 selects the tiny screen editor.  You can move a '+' shaped
  1428.     cursor about the screen using the cursor keys.  The following commands
  1429.     are available:
  1430.  
  1431.         H: Get the current ATTR color from the screen at the cursor's
  1432.            current position, and store it in memory.  This color will be
  1433.            used by the next command:
  1434.         Z: Put the color on the screen
  1435.         G: Get a character from the screen
  1436.         P: Put the character on the screen
  1437.         R: Remove all screen data that is invisible by the ATTR color
  1438.         L: Take a look at the bitmap below the ATTR color codes
  1439.         T: Return to the main menu.  You can also return by pressing
  1440.            EDIT, or ESC in the emulator.
  1441.         B: Change border color
  1442.         V: Clear the whole screen
  1443.  
  1444.     If you press 0, you can edit the current 8x8 character block at pixel
  1445.     level.  Again you control the cursor with the cursor keys.  Now 0
  1446.     toggles a pixel.  In this mode there are two commands: C clears the
  1447.     whole block, and I inverts it.  Pressing EDIT (ESC) returns you to the
  1448.     big screen again.
  1449.  
  1450.     The SamRam has two screen buffers.  Buffer 1 is used to hold the screen
  1451.     which was visible when you pressed NMI, to be able to restore it when
  1452.     returning.  This is the screen you edit with '1'.  The second screen
  1453.     buffer can be used to hold a screen for some time; it is not touched by
  1454.     the NMI software directly, and will not even be destroyed by a Reset. If
  1455.     you press '2', a menu appears with four Dutch entries:
  1456.  
  1457.         1: Scherm 1 opslaan        (Store screen 1 into buffer 2)
  1458.         2: Scherm 2 veranderen     (Edit screen 2)
  1459.         3: Schermen verwisselen    (Swap screens)
  1460.         4: Scherm 2 weghalen       (Remove screen 2)
  1461.  
  1462.     These four functions are rather obvious, I believe.
  1463.  
  1464.     Pressing 'P' pops up the printer menu.  The screendump program is
  1465.     written specifically for my printer, a Star SG-10.  It will probably
  1466.     work on some other printers, but not on most.  The output is sent to the
  1467.     RS232 channel, so you have to redirect it to an LPT output.
  1468.  
  1469.     Skipping the most interesting, 'S', for a moment, let's first discuss
  1470.     the final menu, 'O' for 'Overig', Dutch for miscellaneous.  There are
  1471.     five menu options, of which three are not useful.  The first gives a
  1472.     directory of the cartridge currently in Microdrive 1.  The last, 'E',
  1473.     returns you to Basic if this is anywhere possible: it resets some
  1474.     crucial system variables and generates a Break into Program.  You can
  1475.     use this for instance to break in a BEEP, or crack a not-so-very-well-
  1476.     protected program.  The three other options select normal or speed-
  1477.     save, and store the current setting in CMOS Ram.  Speed-save won't work
  1478.     properly on the emulator, because the speed-save routine toggles the
  1479.     upper 32K ram bank regularly, and this takes too much time on the
  1480.     emulator.  The setting is not important if you use the internal save
  1481.     routine (which will be used by default, unless you select Real Mode).
  1482.  
  1483.     Finally, the 'S' option.  This option allows you to save a snapshot to
  1484.     tape or microdrive.  I used it a lot on my real Spectrum, and it works
  1485.     just as well on the emulator.  It is very useful is you want to load a
  1486.     .Z80 program back into a real Spectrum again.  There are three
  1487.     'switches' you can toggle.  The active choice is indicated by a bright
  1488.     green box, inactive boxes are non-bright.  You have to use EGA or VGA to
  1489.     be able to see it...  The first switch lets you select whether the
  1490.     SamRam rom should be active if the program loads or not.  This is only
  1491.     meaningful is you load it back in a SamRam again.  Usually I want the
  1492.     SamRam rom to be active because I like the character set better.  The
  1493.     second switch indicates whether the SamRam should save a 'loading
  1494.     screen', which it takes from screen buffer 2.  If screen buffer 2
  1495.     contains a screen, this switch will by default be on.  Finally, the last
  1496.     switch lets you select the output media, tape or cartridge.
  1497.  
  1498.     If the program is loaded back into the SamRam, the only bytes that have
  1499.     been corrupted are four bytes down on the stack; this will virtually
  1500.     never be any problem.  If the program is loaded back to a normal
  1501.     Spectrum, these four bytes will also be corrupted, and the bottom two
  1502.     pixel lines of the screen will be filled with data.  (This is
  1503.     considerably less than any other snapshotter I've seen: for instance the
  1504.     Multiface I uses more than 35% of the screen!)
  1505.  
  1506.     The Microdrive BASIC loader needs code in the SamRam rom to start the
  1507.     program (the RANDOMIZE USR 43 calls it).  It won't be very difficult to
  1508.     write a standard BASIC loader that doesn't need this code, but I don't
  1509.     think many people desperately need it...
  1510.  
  1511.  
  1512.  
  1513. 3.3  The built-in monitor
  1514.  
  1515.     This is a really very convenient part of the emulator, and I use it a
  1516.     lot.  It is very MONS-like in its commands and visual appearance.  It
  1517.     cannot single-step however, but on the positive side it has some
  1518.     features MONS hasn't.  It is a part of the SamRam, and cannot therefore
  1519.     be used with Spectrum 128 programs.  If you want to take a look at a
  1520.     Spectrum 128 program, press F10, then change the hardware to SamRam
  1521.     without resetting, and finally generate an NMI in the Extra Functions
  1522.     menu.  You won't probably be able to continue to run the program, but at
  1523.     least you're able to see what it was doing.
  1524.  
  1525.     Press F5 for NMI, and D to enter the monitor/disassembler.  The first
  1526.     eight lines are the first eight instructions, starting at the Memory
  1527.     Pointer, from here on abbreviated by MP.  At first, MP is zero.  The
  1528.     disassembler knows all official instructions, and the SLL instruction.
  1529.     If another inofficial instruction (i.e.  starting with DD, FD or ED) is
  1530.     encountered, the first byte is displayed on a blank line.  The four
  1531.     lines below these display the value of PC and SP, the first nine words
  1532.     on the stack (including AF and the program counter, which have been
  1533.     pushed during NMI), and three MP-memories.  These can be used for
  1534.     temporary storage of the MP, for instance when you take a look at the
  1535.     body of a CALL, and want to return to the main procedure later.
  1536.  
  1537.     The bottom part of the screen displays 24 bytes around the memory
  1538.     pointer.
  1539.  
  1540.     Commands are one letter long; no ENTER needs to be given.  If one or
  1541.     more operands are needed, a colon will appear.  By default the monitor
  1542.     accepts hexadecimal input.  A leading $ denotes that the number is to be
  1543.     regarded as decimal.  If you give the # command, the default will toggle
  1544.     to decimal, and you need to explicitly put a # in front of a number
  1545.     which is to be interpreted as a hex number.  Also, after the # command
  1546.     all addresses on screen will be decimal.  A single character preceded by
  1547.     the " symbol evaluates to its ASCII code, and the single character M
  1548.     will evaluate to the current value of the memory pointer.
  1549.  
  1550.     The monitor commands:
  1551.  
  1552.         Q: Decrease the memory pointer by one.  You effectively shift one
  1553.            byte up.
  1554.         A: Increase the memory pointer, shifting one byte down.
  1555.         ENTER: Shift one instruction down: the memory pointer is
  1556.            increased by the length of first instruction displayed on
  1557.            screen.
  1558.         M: Change the value of the memory pointer.  For instance, M:M
  1559.            won't change it.
  1560.         P: Put.  The word operand supplied will be stored in the first MP
  1561.            memory, and the others will shift on place to the right.
  1562.            Usually, you'll want to store the memory pointer by P:M
  1563.         G: Get.  Typing G:1, G:2 or G:3 moves the value of one of the MP
  1564.            memories to the MP.
  1565.         B: Byte.  This command needs a byte operand; it will be poked
  1566.            into memory, and the memory pointer will move one up.
  1567.         I: Insert.  The same as B, except that you can poke more than one
  1568.            byte.  It continues to ask for bytes to poke until you type
  1569.            Enter on a blank line.
  1570.         #: Toggles the default number base between hexadecimal and
  1571.            decimal.
  1572.         F: Find.  You can enter up to ten bytes, which will be searched
  1573.            through memory.  Searching will stop at address 0, because
  1574.            since the search string is stored in shadow Ram, searching
  1575.            would otherwise not always terminate.  Typing Enter on a blank
  1576.            line starts the search.  Byte operands are entered as usual,
  1577.            but:
  1578.            - If a number bigger than 256 decimal is entered, it is
  1579.              treated as a word in the standard LSB/MSB format.  So, 1234
  1580.              will search for 34,12 hex in that order.  Note that 0012
  1581.              will search for 12, not 12,00.
  1582.            - A line starting with " decodes into the string of characters
  1583.              (up to ten) behind it.  Normally this would only be the
  1584.              first character.  So instead of typing "M "Y "N "A "M "E
  1585.              (space=enter here) you type "MYNAME.  Note that any
  1586.              terminating " will also be searched for!
  1587.            - An x is treated as a wildcard.  So if you search for CD x 80
  1588.              any call to a subroutine in the block 8000-80FF is a hit.
  1589.              If you search for x 8000, you'll see every one-byte
  1590.              instruction that has the address 8000 as operand.
  1591.         N: Continues the search started by F from the current MP.
  1592.         $: Displays one page of disassembly on screen.  In this mode,
  1593.            the following commands are possible:
  1594.            $: Back to the main screen
  1595.            7: [Shift 7 also works, cursor up]: Go to the previous page.
  1596.               The monitor stores the addresses of the previous eight
  1597.               pages only.
  1598.            Q: Go back one byte (decrease MP by one)
  1599.            A: Go one byte forward (increase MP by one)
  1600.            Z: Dump this screen to the printer, in ASCII format.  Redirect
  1601.               the RS232 output to a file, and run CONVERT on it to convert
  1602.               the CR's into CR/LF's before printing (or tell your printer
  1603.               to do the conversion).
  1604.            Every other key displays the next page of disassembly.
  1605.         K: List.  The same mode as with $ is entered, but instead of a
  1606.            disassembly the bytes with their ASCII characters are
  1607.            displayed.  Useful to look for text.
  1608.         C: Clear.  Fills blocks of memory with a specified value.  The
  1609.            monitor prompts with 'First', 'Last' and 'With'.  The 'Last'
  1610.            address is inclusive!
  1611.         D: Dump.  Prompts with 'First' and 'Last', and dumps a
  1612.            disassembly of the block between these addresses to the
  1613.            printer.  See remark at $-Z.  The 'Last' address is again
  1614.            inclusive.
  1615.         R: Registers.  If you press Enter after R, an overview of the
  1616.            registers contents is displayed.  If you type one of A,B,C,D,
  1617.            E,H,L,A',B',C',D',E',H',L',I,R,AF,BC,DE,HL,AF',BC',DE',HL',
  1618.            IX,IY,SP or PC, you can change the value of it.  Changing the
  1619.            value of SP also changes the PC and AF values by the way.  You
  1620.            cannot change the Interrupt mode or IFF.
  1621.         V: Verplaats.  (Move).  Prompts with 'From', 'To' and 'Length'.
  1622.            Obvious.
  1623.         S: Save.  Enter the start of the block you wish to save first.
  1624.            The monitor then prompts with 'Length'.  The block is saved
  1625.            without a header, as a normal data block (A, the flagbyte, is
  1626.            0FF)
  1627.         L: Load.  Loads a block of data from tape, at the specified
  1628.            address.  Normal data blocks, headers and blocks with non-
  1629.            standard flag bytes can be loaded.  The first byte in memory
  1630.            will contain the flag byte.  If the checksum isn't 0 after
  1631.            loading, indicating a tape error, you'll hear a beep.
  1632.         H: Header read.  Loads headers and displays the contents on
  1633.            screen.
  1634.  
  1635.  
  1636.     As you're reading this part, I assume you know something of machine
  1637.     code.  Probably you would be interested in peeking into the software of
  1638.     the SamRam, the Interface I or the Spectrum 128.  You'll first have to
  1639.     move these roms in ram to be able to look at them with the monitor.
  1640.  
  1641.     The Interface I rom can be moved into ram by saving it to microdrive or
  1642.     to the "b" channel, with SAVE *"m";1;"rom" CODE 0,8192 or SAVE *"b" CODE
  1643.     0,8192, and loading it back again at 32768 for instance.  You can also
  1644.     put this small machine code routine at 23296 and run it: F3 21 0C 5B E5
  1645.     21 00 00 E5 C3 08 00 21 00 00 11 00 80 01 00 20 ED B0 FB C3 00 07.
  1646.  
  1647.     The two SamRam roms are easy.  The first you don't need to transfer; the
  1648.     monitor looks at the extended basic rom by default.  The second rom can
  1649.     be moved to 32768 by typing *SAVE 0,32768,16384.  (The SAVE is not the
  1650.     keyword SAVE!)
  1651.  
  1652.     The first '128 rom, the one which is active at reset and contains most
  1653.     of the new code, is moved up by typing SAVE!"rom"CODE 0,16384, then
  1654.     LOAD!"rom"CODE 32768.  The other rom is most conveniently moved by
  1655.     saving it to a .TAP file and loading it back again in ram.  To select
  1656.     the SamRam type SPECTRUM first, and then switch the hardware without
  1657.     resetting.
  1658.  
  1659.  
  1660.  
  1661.  
  1662. 4.  THE SPECTRUM
  1663.  
  1664.  
  1665. 4.1  The Spectrum
  1666.  
  1667.     This emulator supports the Interface I and the Spectrum 128.  Many
  1668.     Spectrum users will have no experience with them, so some comments may
  1669.     be useful.  On the other hand, I don't think this is the right place to
  1670.     describe the Spectrum Basic in full detail.  If you want to know it all,
  1671.     read the official manuals!
  1672.  
  1673.     If you want to use Spectrum Basic, you will need the keywords.  You
  1674.     could by the way now also use the Spectrum 128 Basic where you can type
  1675.     the keywords in by full.
  1676.  
  1677.     If you press ALT-F1 in the emulator, the Spectrum keyboard layout will
  1678.     appear.  For completeness I include an alphabetical list of all keywords
  1679.     and their key-combination.  In the list below, K stands for Keyword
  1680.     mode, E for E-mode (type Shift-Alt of Shift-Ctrl to select E-mode), S
  1681.     for Symbol Shift, and SE for Symbol Shifted (Alt/Ctrl) E-mode: select E
  1682.     mode and type the letter while depressing Symbol Shift.
  1683.  
  1684.  
  1685.         Keyw.    Code | Keyw.    Code | Keyw.    Code | Keyw.    Code
  1686.  
  1687.         ABS      E g    DRAW     K w    MERGE    SE t   SAVE     K s
  1688.         ACS      SE w   ERASE    SE 7   MOVE     SE 6   SCREEN$  SE k
  1689.         AND      S y    EXP      E x    NEW      K a    SGN      E f
  1690.         ASN      SE q   FLASH    SE v   NEXT     K n    SIN      E q
  1691.         AT       S i    FN       SE 2   NOT      S s    SQR      E h
  1692.         ATN      SE e   FOR      K f    OPEN #   SE 4   STEP     S d
  1693.         ATTR     SE l   FORMAT   SE 0   OR       S u    STOP     S a
  1694.         BEEP     SE z   GO SUB   K h    OUT      SE o   STR$     E y
  1695.         BIN      E b    GO TO    K g    OVER     SE n   TAB      E p
  1696.         BORDER   K b    IF       K u    PAPER    SE c   TAN      E e
  1697.         BRIGHT   SE b   IN       SE i   PAUSE    K m    THEN     S g
  1698.         CAT      SE 9   INK      SE x   PEEK     E o    TO       S f
  1699.         CHR$     E u    INKEY$   E n    PI       E m    USR      E l
  1700.         CIRCLE   SE h   INPUT    K i    PLOT     K q    VAL      E j
  1701.         CLEAR    K x    INT      E r    POINT    SE 8   VAL$     SE j
  1702.         CLOSE #  SE 5   INVERSE  SE m   POKE     K o    VERIFY   SE r
  1703.         CLS      K v    LEN      E k    PRINT    K p    <=       S q
  1704.         CODE     E i    LET      K l    RANDOMIZE K t   >=       S e
  1705.         CONTINUE K c    LIST     K k    READ     E a    <>       S w
  1706.         COPY     K z    LINE     SE 3   REM      K e
  1707.         COS      E w    LLIST    E v    RESTORE  E s    DEC      SE q
  1708.         DATA     E d    LN       E z    RETURN   K y    HEX      SE w
  1709.         DEF FN   SE 1   LOAD     K j    RND      E t
  1710.         DIM      K d    LPRINT   E c    RUN      K r
  1711.  
  1712.  
  1713.         Character  Spectrum kbrd   On PC keyboard
  1714.  
  1715.         &          S 6             ALT (or CTRL) 6
  1716.         '          S 7             ALT 7 or '/"
  1717.         (          S 8             ALT 8
  1718.         )          S 9             ALT 9
  1719.         _          S 0             ALT 0 or SHFT _/-
  1720.         <          S r             ALT r or SHFT </,
  1721.         >          S t             ALT t or SHFT >/,
  1722.         ;          S o             ALT o or :/;
  1723.         "          S p             ALT p or SHFT "/'
  1724.         ^          S h             ALT h
  1725.         -          S j             ALT j or _/-
  1726.         +          S k             ALT k or SHFT +/= or GREY +
  1727.         =          S l             ALT l or +/=
  1728.         :          S z             ALT z or SFHT :/;
  1729.         ?          S c             ALT c or SHFT ?//
  1730.         /          S v             ALT v or ?//
  1731.         *          S b             ALT b or GREY PRTSC/*
  1732.         ,          S n             ALT n or </,
  1733.         .          S m             ALT m or >/.
  1734.  
  1735.  
  1736.  
  1737. 4.2   The Interface I
  1738.  
  1739.     If you want to use the microdrive, you'll need cartridge files.  The
  1740.     emulator can create an empty cartridge file for you.  You have to format
  1741.     it before you can use it.  Type
  1742.  
  1743.         FORMAT "m";1;"name"
  1744.  
  1745.     to format the cartridge currently in Microdrive 1 giving it the name
  1746.     'name'.  Next, type CAT 1 to get a catalogue of the files on it (none of
  1747.     course) and the number of kilobytes free.  You can save a file by typing
  1748.     for instance
  1749.  
  1750.         SAVE *"m";1;"screen"SCREEN$
  1751.  
  1752.     Instead of SCREEN$ you can use all other expressions that are permitted
  1753.     also when saving to tape, like LINE nnnn or CODE x,y etcetera.  To load
  1754.     a file back from cartridge, you type (you guessed it)
  1755.  
  1756.         LOAD *"m";1;"screen"SCREEN$
  1757.  
  1758.     If the file doesn't exist or is of the wrong type you'll get the
  1759.     appropriate error message.  To erase a file, type for instance
  1760.  
  1761.         ERASE "m";1;"screen"
  1762.  
  1763.     Note that no * is needed (or even permitted), and that only the name
  1764.     should be given.  There's another way to create a file on a cartridge,
  1765.     and that is by using a command like OPEN #3;"m";1;"name", and printing
  1766.     to that stream.  You can use MOVE to move data from stream to stream,
  1767.     but I'll not go into that --- it's not very much used anyway.
  1768.  
  1769.     Instead of to the microdrive, you can also 'save to the RS232 link'. For
  1770.     instance, type SAVE *"b"SCREEN$ (note: there's no name!) to save a
  1771.     screen.  On the emulator you can send the output to the RS232 channel to
  1772.     a printer (then SAVE *"b" is useless), to a file (can be useful) or to
  1773.     the COM port (very useful if you connect a real Spectrum to the PC's COM
  1774.     port!).  You can load the data back by typing LOAD *"b"SCREEN$ and
  1775.     making sure the RS232 channel is fed with the right input (from a COM
  1776.     port or a file).  See also paragraph 2.6.
  1777.  
  1778.     If you want to use the RS232 channel for printing, open stream 3 for
  1779.     output to that channel by typing
  1780.  
  1781.         OPEN #3,"b"
  1782.  
  1783.     or
  1784.  
  1785.         OPEN #3,"t"
  1786.  
  1787.     The first will simply copy everything you send to stream 3 (using for
  1788.     instance LPRINT or LLIST) to the RS232 channel; the second converts CR's
  1789.     into CR/LF's, breaks off lines at 80 characters and translates keywords
  1790.     into character sequences.  "t" is useful for LLISTings, but not for
  1791.     anything else.
  1792.  
  1793.     Useful extra commands: CLS #, to clear the screen and reset the
  1794.     attributes to their reset defaults, and CLEAR # to do a CLS # and close
  1795.     all currently open streams (discarding all data that may still be
  1796.     buffered!)
  1797.  
  1798.     The Interface I uses its own system variables.  At the first error
  1799.     message you make (or RASP, or flashing question mark) and at the first
  1800.     Interface I statement you execute, it inserts them automatically.  Some
  1801.     programs will not run when the Interface I has inserted its system
  1802.     variables.  So if you load a game from tape, reset the Spectrum first
  1803.     and don't make an error typing LOAD "".  With a bit of exercise you
  1804.     should be able to do this.
  1805.  
  1806.  
  1807.  
  1808. 4.3  The Spectrum 128
  1809.  
  1810.     The main new features of the Spectrum 128 are its larger memory, that
  1811.     can be used as a Ram drive in Basic, and music capabilities.
  1812.  
  1813.     The Ram drive is accessed via the LOAD!, SAVE!, ERASE! and CAT!
  1814.     commands.  They work as you would expect.  Examples:
  1815.  
  1816.         SAVE !"name"SCREEN$
  1817.         CAT!
  1818.         LOAD !"name"SCREEN$
  1819.         ERASE !"name"
  1820.  
  1821.     The 3 channel sound chip of the Spectrum 128 can be used in Basic with
  1822.     the PLAY command.  Example:
  1823.  
  1824.         PLAY "cde","efg","gAB"
  1825.  
  1826.     plays three chords.  You can program complex effects, melodies and
  1827.     rhythms with the play command; they require many commands in the three
  1828.     voice strings which I won't explain...  They are explained in the
  1829.     Spectrum 128's user guide.
  1830.  
  1831.  
  1832.  
  1833.  
  1834. 5.  TECHNICAL INFORMATION
  1835.  
  1836.  
  1837. 5.1  The Spectrum
  1838.  
  1839.     The Spectrum is at the hardware level a very simple machine.  There's
  1840.     the 16K ROM which occupies the lowest part of the address space, and 48K
  1841.     of RAM which fills up the rest.  An ULA which reads the lowest 6912
  1842.     bytes of RAM to display the screen, and contains the logic for just one
  1843.     I/O port completes the machine, from a software point of view at least.
  1844.  
  1845.     Every even I/O address will address the ULA, but to avoid problems with
  1846.     other I/O devices only port FE should be used.  If this port is written
  1847.     to, bits have the following meaning:
  1848.  
  1849.  
  1850.         Bit   7   6   5   4   3   2   1   0
  1851.             ┌───┬───┬───┬───┬───┬───┬───┬───┐
  1852.             │   │   │   │ E │ M │   Border  │
  1853.             └───┴───┴───┴───┴───┴───┴───┴───┘
  1854.  
  1855.  
  1856.     The lowest three bits specify the border colour; a zero in bit 3
  1857.     activates the MIC output, and a one in bit 4 activates the EAR output
  1858.     (which sounds the internal speaker).  The real Spectrum also activates
  1859.     the MIC when the ear is written to; the emulator doesn't.  This is no
  1860.     problem; MIC is only used for saving, and when saving the Spectrum never
  1861.     sounds the internal speaker.  The upper three bits are unused.
  1862.  
  1863.     If port FE is read from, the highest eight address lines are important
  1864.     too.  A zero on one of these lines selects a particular half-row of five
  1865.     keys:
  1866.  
  1867.       IN:    Reads keys (bit 0 to bit 4 inclusive)
  1868.  
  1869.       #FEFE  SHIFT, Z, X, C, V            #EFFE  0, 9, 8, 7, 6
  1870.       #FDFE  A, S, D, F, G                #DFFE  P, O, I, U, Y
  1871.       #FBFE  Q, W, E, R, T                #BFFE  ENTER, L, K, J, H
  1872.       #F7FE  1, 2, 3, 4, 5                #7FFE  SPACE, SYM SHFT, M, N,
  1873.  
  1874.     A zero in one of the five lowest bits means that the corresponding key
  1875.     is being pressed.  If more than one address line is made low, the result
  1876.     is the logical AND of all single inputs, so a zero in a bit means that
  1877.     at least one of the appropriate keys is pressed.  For example, only if
  1878.     each of the five lowest bits of the result from reading from port 00FE
  1879.     (for instance by XOR A/IN A,(FE)) is one, no key is pressed.
  1880.  
  1881.     A final remark about the keyboard.  It is connected in a matrix-like
  1882.     fashion, with 8 rows of 5 columns, as is obvious from the above remarks.
  1883.     Any two keys pressed simultaneously can be uniquely decoded by reading
  1884.     from the IN ports, however, if more than two keys are pressed decoding
  1885.     may not be uniquely possible.  For instance, if you press Caps shift, B
  1886.     and V, the Spectrum will think also the Space key is pressed, and react
  1887.     by giving the 'Break into Program' report.  This matrix behaviour is
  1888.     also emulated - without it, Zynaps for instance won't pause when you
  1889.     press 5,6,7,8 and 0 simultaneously.
  1890.  
  1891.     Bit 5 (value 64) of IN-port FE is the ear input bit.  When the line is
  1892.     silent, its value is zero, except in the early Model 2 of the Spectrum,
  1893.     where it was one.  When there is a signal, this bit toggles.  The
  1894.     Spectrum loading software is not sensitive to the polarity of this bit
  1895.     (which it definitely should not be, not only because of this model
  1896.     difference, but also because you cannot be sure the tape recorder
  1897.     doesn't change the polarity of the signal recorded!) Some old programs
  1898.     rely on the fact that bit 5 is always one (for instance Spinads); for
  1899.     these programs the emulator can mimic a Model 2 Spectrum.
  1900.  
  1901.     Bits 6 and 7 are always one.
  1902.  
  1903.     The ULA with the lower 16K of RAM, and the processor with the upper 32K
  1904.     RAM and 16K ROM are working independently of each other.  The data and
  1905.     address buses of the Z80 and the ULA are connected by small resistors;
  1906.     normally, these do effectively decouple the buses.  However, if the Z80
  1907.     wants to read of write the lower 16K, the ULA halts the processor if it
  1908.     is busy reading, and after it's finished it lets the processor access
  1909.     lower memory through the resistors.  A very fast, cheap and neat design
  1910.     indeed!
  1911.  
  1912.     If you run a program in the lower 16K of RAM, or read or write in that
  1913.     memory, the processor is halted sometimes.  This part of memory is
  1914.     therefore somewhat slower than the upper 32K block.  This is also the
  1915.     reason that you cannot write a sound- or save-routine in lower memory;
  1916.     the timing won't be exact, and the music will sound harsh.  Also, INning
  1917.     from port FE will halt the processor, because the ULA has to supply the
  1918.     result.  Therefore, INning from port FE is a tiny bit slower on average
  1919.     than INning from other ports; whilst normally an IN A,(nn) instruction
  1920.     would take 11 T states, it takes 12.15 T states on average if nn=FE. See
  1921.     below for more exact information.
  1922.  
  1923.     If the processor reads from a non-existing IN port, for instance FF, the
  1924.     ULA won't stop, but nothing will put anything on the data bus.
  1925.     Therefore, you'll read a mixture of FF's (idle bus), and screen and ATTR
  1926.     data bytes (the latter being very scarce, by the way).  This will only
  1927.     happen when the ULA is reading the screen memory, about 60% of the
  1928.     1/50th second time slice in which a frame is generated.  The other 40%
  1929.     the ULA is building the border or generating a vertical retrace.  This
  1930.     behaviour is actually used in some program, for instance by Arkanoid,
  1931.     and the emulator also emulates this behaviour.
  1932.  
  1933.     Finally, there is an interesting bug in the ULA which also has to do
  1934.     with this split bus.  After each instruction fetch cycle of the
  1935.     processor, the processor puts the I-R register 'pair' (not the 8 bit
  1936.     internal Instruction Register, but the Interrupt and R registers) on the
  1937.     address bus.  The lowest 7 bits, the R register, are used for memory
  1938.     refresh.  However, the ULA gets confused if I is in the range 64-127,
  1939.     because it thinks the processor wants to read from lower 16K ram very,
  1940.     very often.  The ULA can't cope with this read-frequency, and regularly
  1941.     misses a screen byte.  Instead of the actual byte, the byte previously
  1942.     read is used to build up the video signal.  The screen seems to be
  1943.     filled with 'snow'; however, the Spectrum won't crash, and program will
  1944.     continue to run normally.  There's one program I know of that uses this
  1945.     to generate a nice effect: Vectron.  (which has very nice music too by
  1946.     the way).  This effect has not been implemented however - it's a bit
  1947.     useless (but maybe I'll include it in the future).
  1948.  
  1949.     The processor has three interrupt modes, selected by the instructions IM
  1950.     0, IM 1 and IM 2.  In mode 1, the processor simply executes a RST #38
  1951.     instruction if an interrupt is requested.  This is the mode the Spectrum
  1952.     is normally in.  The other mode that is commonly used is IM 2.  If an
  1953.     interrupt is requested, the processor first builds a 16 bit address by
  1954.     combining the I register (as the high byte) with whatever the
  1955.     interrupting device places on the data bus.  The word at this address is
  1956.     then called.  Rodnay Zaks in his book 'Programming the Z80' states that
  1957.     only even bytes are allowed as low index byte, but that isn't true.  The
  1958.     normal Spectrum contains no hardware to place a byte on the bus, and the
  1959.     bus will therefore always read FF (because the ULA also doesn't read the
  1960.     screen if it generates an interrupt), so the resulting index address is
  1961.     256*I+0FF.  However, some not-so-neat hardware devices put things on the
  1962.     data bus when they shouldn't, so later programs didn't assume the low
  1963.     index byte was 0FF.  These programs contain a 257 byte table of equal
  1964.     bytes starting at 256*I, and the interrupt routine is placed at an
  1965.     address that is a multiple of 257.  A useful but not so much used trick
  1966.     is to make the table contain FF's (or use the ROM for this) and put a
  1967.     byte 18 hex, the opcode for JR, at FFFF.  The first byte of the ROM is a
  1968.     DI, F3 hex, so the JR will jump to FFF4, where a long JP to the actual
  1969.     interrupt routine is put.
  1970.  
  1971.     In interrupt mode 0, the processor executes the instruction that the
  1972.     interrupting device places on the data bus.  On a standard Spectrum this
  1973.     will be the byte FF, coincidentally (...) the opcode for RST #38. But
  1974.     for the same reasons as above, this is not really reliable.
  1975.  
  1976.     The 50 Hz interrupt is synchronized with the video signal generation by
  1977.     the ULA; both the interrupt and the video signal are generated by it.
  1978.     Many programs use the interrupt to synchronize with the frame cycle.
  1979.     Some use it to generate fantastic effects, such as full-screen
  1980.     characters, full-screen horizon (Aquaplane) or pixel colour (Uridium for
  1981.     instance).  Very many modern programs use the fact that the screen is
  1982.     'written' (or 'fired') to the CRT in a finite time to do as much
  1983.     time-consuming screen calculations as possible without causing character
  1984.     flickering:  although the ULA has started displaying the screen for this
  1985.     frame already, the electron beam will for a moment not 'pass'
  1986.     this-or-that part of the screen so it's safe to change something there.
  1987.     So the exact time in the 1/50 second time-slice at which the screen is
  1988.     updated is very important.  Because the emulator updates the screen at
  1989.     once, no single best solution can be given, and therefore the user can
  1990.     select one of three possibilities (low, normal or high video
  1991.     synchronisation, corresponding to a screen update after 1/200, 2/200 or
  1992.     3/200 of a (relative) second after a Z80 interrupt) which gives the best
  1993.     results.  Try for instance Zynaps; with normal video synchronisation the
  1994.     top four or five lines of the background move out-of-phase with the
  1995.     rest, and your space-ship flickers in that region.  With low video
  1996.     synchronisation the background moves smoothly but the sprites flicker in
  1997.     all parts of the screen.  Only with high video sync everything moves
  1998.     smoothly and doesn't flicker.
  1999.  
  2000.     This emulator does not try to emulate the really time-critical border
  2001.     pattern effects (except when loading, but the width of the loading
  2002.     stripes are not quite right because also PC video timings come into
  2003.     play), but maybe I'll include it in the future.  I will need some hard
  2004.     data on video timings then, and I've figured these out recently.  Here
  2005.     they are.
  2006.  
  2007.     Each line takes exactly 224 T states.  After an interrupt occurs, 64
  2008.     line times pass before the byte 16384 is displayed.  At least the last
  2009.     48 of these are actual border-lines.  I could not determine whether my
  2010.     monitor didn't display the others or whether it was in vertical retrace,
  2011.     but luckily that's not really important.  Then the 192 screen+border
  2012.     lines are displayed, followed by about 56 border lines again.  56.5
  2013.     border lines would make up exactly 70000 T states, 1/50th of 3500000.
  2014.     However, I noticed that the frequency of the 50 Hz interrupt (measured
  2015.     in 1/T states!) changes very slightly when my Spectrum gets hot (I think
  2016.     it has something to do with the relative change of the frequencies of
  2017.     the two crystals in the Spectrum), so the time between interrupts will
  2018.     probably not be exactly 70000 T states. Anyway, whether the final border
  2019.     block is of fixed or variable length doesn't concern us either, the
  2020.     timings of the start and end of the screen, which are the timings of
  2021.     real interest, are fixed.
  2022.  
  2023.     Now for the timings of each line itself.  I define a screen line to
  2024.     start with 256 screen pixels, then border, then horizontal retrace, and
  2025.     then border again.  All this takes 224 T states.  Every half T state a
  2026.     pixel is written to the CRT, so if the ULA is reading bytes it does so
  2027.     each 4 T states (and then it reads two: a screen and an ATTR byte).  The
  2028.     border is 48 pixels wide at each side.  A video screen line is therefore
  2029.     timed as follows: 128 T states of screen, 24 T states of right border,
  2030.     48 T states of horizontal retrace and 24 T states of left border.
  2031.  
  2032.     When an interrupt occurs, the running instruction has to be completed
  2033.     first.  So the start of the interrupt is fixed relative to the start of
  2034.     the frame up to the length of the last instruction in T states.  If the
  2035.     processor was executing a HALT (which, according to the Z80 books I
  2036.     read, is effectively many NOPs), the interrupt routine starts at most 3
  2037.     T states away from the start of the frame.  Of course the processor also
  2038.     needs some T states to store the program counter on the stack, read the
  2039.     interrupt vector and jump to the routine, but since I cannot determine
  2040.     that by only using the Spectrum, it is useless information by that very
  2041.     reason alone!
  2042.  
  2043.     Now when to OUT to the border to change it at the place you want? First
  2044.     of all, you cannot change the border within a 'byte', an 8-pixel chunk.
  2045.     If we forget about the screen for a moment, if you OUT to port FE after
  2046.     14326 to 14329 T states (including the OUT) from the start of the IM 2
  2047.     interrupt routine, the border will change at exactly the position of
  2048.     byte 16384 of the screen.  The other positions can be computed by
  2049.     remembering that 8 pixels take 4 T states, and a line takes 224 T
  2050.     states.  You would think that OUTing after 14322 to 14325 T states, the
  2051.     border would change at 8 pixels left of the upper left corner of the
  2052.     screen.  This is right for 14322, 14323 and 14324 T states, but if you
  2053.     wait 14325 T states the ULA happens to be reading byte 16384 (or 22528,
  2054.     or both) and will halt the processor for a while, thereby making you
  2055.     miss the 8 pixels.  This exception happens again after 224 T states, and
  2056.     again after 448, an so forth.  These 192 exceptions left of the actual
  2057.     screen rectangle are the only ones; similar things don't happen at the
  2058.     right edge because the ULA don't need to read things there - it has just
  2059.     finished!
  2060.  
  2061.     As noted above, reading or writing in low ram (or OUTing to the ULA)
  2062.     causes the ULA to halt the processor.  When and how much? The processor
  2063.     is halted each time you want to access the ULA or low memory and the ULA
  2064.     is busy reading.  Of the 312.5 'lines' the ULA generates, only 192
  2065.     contain actual screen pixels, and the ULA will only read bytes during
  2066.     128 of the 224 T states of each screen line.  But if it does, the
  2067.     processor is halted for exactly 4 T states.
  2068.  
  2069.  
  2070.  
  2071. 5.2  The Interface I
  2072.  
  2073.     The Interface I is quite complicated.  It uses three different I/O
  2074.     ports, and contains logic to page and unpage an 8K ROM if new commands
  2075.     are used.  I won't be very detailed here; you could refer to the source
  2076.     code of the emulator if you want to know some details, or read the
  2077.     'Spectrum Shadow ROM Disassembly' by Gianlura Carri, published by
  2078.     Melbourne House - but don't expect the same level of detail as of Ian
  2079.     Logan and Frank O'Hara in their Rom disassembly book.
  2080.  
  2081.     The ROM is paged if the processor executes the instruction at ROM
  2082.     address 0008 or 1708 hexadecimal, the error and close# routines.  It is
  2083.     inactivated when the Z80 executes the RET at address 0700.
  2084.  
  2085.     I/O Port E7 is used to send or receive data to and from the microdrive.
  2086.     Accessing this port will halt the Z80 until the Interface I has
  2087.     collected 8 bits from the microdrive head; therefore, it the microdrive
  2088.     motor isn't running, or there is no formatted cartridge in the
  2089.     microdrive, the Spectrum hangs.  This is the famous 'IN 0 crash'.
  2090.  
  2091.     Port EF is used for several things:
  2092.  
  2093.  
  2094.        Bit    7   6    5    4    3    2    1     0
  2095.             ┌───┬───┬────┬────┬─────┬───┬─────┬─────┐
  2096.         READ│   │   │    │busy│ dtr │gap│ sync│write│
  2097.             │   │   │    │    │     │   │     │prot.│
  2098.             ├───┼───┼────┼────┼─────┼───┼─────┼─────┤
  2099.        WRITE│   │   │wait│ cts│erase│r/w│comms│comms│
  2100.             │   │   │    │    │     │   │ clk │ data│
  2101.             └───┴───┴────┴────┴─────┴───┴─────┴─────┘
  2102.  
  2103.  
  2104.     Bits DTR and CTS are used by the RS232 interface.  The WAIT bit is used
  2105.     by the Network to synchronise, GAP, SYNC, WR_PROT, ERASE, R/_W, COMMS
  2106.     CLK and COMMS DATA are used by the microdrive system.  If the microdrive
  2107.     is not being used, the COMMS DATA output selects the function of bit 0
  2108.     of out-port F7:
  2109.  
  2110.  
  2111.        Bit      7    6   5   4   3   2   1       0
  2112.             ┌──────┬───┬───┬───┬───┬───┬───┬───────────┐
  2113.         READ│txdata│   │   │   │   │   │   │    net    │
  2114.             │      │   │   │   │   │   │   │   input   │
  2115.             ├──────┼───┼───┼───┼───┼───┼───┼───────────┤
  2116.        WRITE│      │   │   │   │   │   │   │net output/│
  2117.             │      │   │   │   │   │   │   │   rxdata  │
  2118.             └──────┴───┴───┴───┴───┴───┴───┴───────────┘
  2119.  
  2120.  
  2121.     TXDATA and RXDATA are the input and output of the RS232 port.  COMMS
  2122.     DATA determines whether bit 0 of F7 is output for the RS232 or the
  2123.     network.
  2124.  
  2125.  
  2126.  
  2127. 5.3  The SamRam
  2128.  
  2129.     The SamRam contains a 32K static CMOS Ram chip, and some I/O logic for
  2130.     port 31.  If this port is read, it returns the position of the joystick,
  2131.     as a normal Kempston joystickinterface would.  If written to, the port
  2132.     controls a programmable latch chip (the 74LS259) which contains 8
  2133.     latches:
  2134.  
  2135.  
  2136.        Bit    7   6   5   4   3   2   1   0
  2137.             ┌───┬───┬───┬───┬───┬───┬───┬───┐
  2138.        WRITE│   │   │   │   │  address  │bit│
  2139.             └───┴───┴───┴───┴───┴───┴───┴───┘
  2140.  
  2141.  
  2142.     The address selects on of the eight latches; bit 0 is the new state of
  2143.     the latch.  The 16 different possibilities are collected in the diagram
  2144.     below:
  2145.  
  2146.         OUT 31,   │  Latch  │ Result
  2147.         ──────────┼─────────┼────────────────────────────────────────
  2148.             0     │    0    │ Switch on write protect of CMOS RAM
  2149.             1     │    "    │ Writes to CMOS RAM allowed
  2150.             2     │    1    │ turn on CMOS RAM (see also 6/7)
  2151.             3     │    "    │ turn off CMOS RAM (standard Spec.  ROM)
  2152.             4     │    2    │ -
  2153.             5     │    "    │ Ignore all OUT's to 31 hereafter
  2154.             6     │    3    │ Select CMOS bank 0 (Basic ROM)
  2155.             7     │    "    │ Select CMOS bank 1 (Monitor,...)
  2156.             8     │    4    │ Select interface 1
  2157.             9     │    "    │ Turn off IF 1 (IF1 rom won't be paged)
  2158.            10     │    5    │ Select 32K ram bank 0 (32768-65535)
  2159.            11     │    "    │ Select 32K ram bank 1 (32768-65535)
  2160.            12     │    6    │ Turn off beeper
  2161.            13     │    "    │ Turn on beeper
  2162.            14     │    7    │ -
  2163.            15     │    "    │ -
  2164.  
  2165.     At reset, all latches are 0.  If an OUT 31,5 is issued, only a reset
  2166.     will give you control over the latches again.  The write protect latch
  2167.     is not emulated; you're never able to write the emulated CMOS ram in the
  2168.     emulator.  Latch 4 will pull up the M1 output of the Z80.  The Interface
  2169.     I won't page the ROM anymore then.
  2170.  
  2171.  
  2172.  
  2173. 5.4  The Z80 microprocessor
  2174.  
  2175.     The Z80 processor is quite straightforward, and contains to my knowledge
  2176.     no interesting bugs or quirks.  However, it has some undocumented
  2177.     features.  Some of these are quite useful, and some are not, but since
  2178.     many programs use the useful ones, and a few programs use the weird
  2179.     ones, I tried to figure them out and emulate them as best as I could.
  2180.     There is a Z80 emulator around, intended as a CP/M emulator, which halts
  2181.     the program if an undocumented opcode is encountered.  I don't think
  2182.     this makes sense.  ZiLOG doesn't dictate the law, the programs which use
  2183.     the processor's features do!
  2184.  
  2185.     Most Z80 opcodes are one byte long, not counting a possible byte or word
  2186.     operand.  The four opcodes CB, DD, ED and FD are shift opcodes: they
  2187.     change the meaning of the opcode following them.
  2188.  
  2189.     There are 248 different CB opcodes.  The block CB 30 to CB 37 is missing
  2190.     from the official list.  These instructions, usually denoted by the
  2191.     mnemonic SLL, Shift Left Logical, shift left the operand and make bit 0
  2192.     always one.  Bounder and Enduro Racer use them.  The SamRam monitor can
  2193.     disassemble these and uses the mnemonic SLL.  These instructions are
  2194.     quite commonly used.
  2195.  
  2196.     The DD and FD opcodes precede instructions using the IX and IY
  2197.     registers.  If you look at the instructions carefully, you see how they
  2198.     work:
  2199.  
  2200.         2A nn           LD HL,(nn)
  2201.         DD 2A nn        LD IX,(nn)
  2202.         7E              LD A,(HL)
  2203.         DD 7E d         LD A,(IX+d)
  2204.  
  2205.     A DD opcode simply changes the meaning of HL in the next instruction.
  2206.     If a memory byte is addressed indirectly via HL, as in the second
  2207.     example, a displacement byte is added.  Otherwise the instruction simply
  2208.     acts on IX instead of HL.  (A notational awkwardness, that will only
  2209.     bother assembler and disassembler writers: JP (HL) is not indirect; it
  2210.     should have been denoted by JP HL) If a DD opcode precedes an
  2211.     instruction that doesn't use the HL register pair at all, the
  2212.     instruction is executed as usual.  However, if the instruction uses the
  2213.     H or L register, it will now use the high or low halves of the IX
  2214.     register! Example:
  2215.  
  2216.         44              LD B,H
  2217.         FD 44           LD B,IYh
  2218.  
  2219.     These types of inofficial instructions are used by very many programs.
  2220.     By the way, many DD or FD opcodes after each other will effectively be
  2221.     NOPs, doing nothing except repeatedly setting the flag 'treat HL as IX'
  2222.     (or IY) and taking up 4 T states.  (But try to let MONS disassemble such
  2223.     a block.)
  2224.  
  2225.     I've never seen a program using inofficial ED instructions, and except
  2226.     for ED 6B nn, a long version of 2A nn, LD HL,(nn) I don't know any.  I
  2227.     am pretty sure however that they exist, but I never took the trouble to
  2228.     test them all.
  2229.  
  2230.     About the R register.  This is not really an undocumented feature,
  2231.     although I have never seen any thorough description of it anywhere.  The
  2232.     R register is a counter that is updated every instruction, where DD, FD,
  2233.     ED and CB are to be regarded as separate instructions.  So shifted
  2234.     instruction will increase R by two.  There's an interesting exception:
  2235.     doubly-shifted opcodes, the DDCB and FDCB ones, increase R by two too.
  2236.     LDI increases R by two, LDIR increases it by 2 times BC, as does LDDR
  2237.     etcetera.  The sequence LD R,A / LD A,R increases A by two, except for
  2238.     the highest bit: this bit of the R register is never changed.  This is
  2239.     because in the old days everyone used 16 Kbit chips.  Inside the chip
  2240.     the bits where grouped in a 128x128 matrix, needing a 7 bit refresh
  2241.     cycle.  Therefore ZiLOG decided to count only the lowest 7 bits. Anyway,
  2242.     if the R register emulation is switched on the R register will behave as
  2243.     is does on a real Spectrum; if it is off it will (except for the upper
  2244.     bit) act as a random generator.
  2245.  
  2246.     You can easily check that the R register is really crucial to memory
  2247.     refresh.  Assemble this program:
  2248.  
  2249.         ORG 32768
  2250.         DI
  2251.         LD B,0
  2252.     L1  XOR A
  2253.         LD R,A
  2254.         DEC HL
  2255.         LD A,H
  2256.         OR L
  2257.         JR NZ,L1
  2258.         DJNZ L1
  2259.         EI
  2260.         RET
  2261.  
  2262.     It will take about three minutes to run.  Look at the upper 32K of
  2263.     memory, for instance the UDG graphics.  It will have faded.  Only the
  2264.     first few bytes of each 256 byte block will still contain zeros, because
  2265.     they were refreshed during the execution of the loop.  The ULA took care
  2266.     of the refreshing of the lower 16K.  (This example won't work on the
  2267.     emulator of course!)
  2268.  
  2269.     Then there's one other dark corner of the Z80 which has its effect on
  2270.     programs like Sabre Wulf, Ghosts'n Goblins and Speedlock.  The Mystery
  2271.     of the Undocumented Flags!
  2272.  
  2273.     Bit 3 and 5 of the F register are not used.  They can contain
  2274.     information, as you can readily figure out by using PUSH AF and POP AF.
  2275.     Furthermore, sometimes their values change.  I found the following
  2276.     empirical rule:
  2277.  
  2278.         The values of bit 7, 5 and 3 follow the values of the
  2279.         corresponding bits of the last 8 bit result of an instruction
  2280.         that changed the usual flags.
  2281.  
  2282.     For instance, after an ADD A,B those bits will be identical to the bits
  2283.     of the A register.  (Bit 7 of F is the sign flag, and fits the rule
  2284.     exactly).  An exception is the CP x instruction (x=register, (HL) or
  2285.     direct argument).  In that case the bits are copied from the argument.
  2286.  
  2287.     If the instruction is one that operates on a 16 bit word, the 8 bits of
  2288.     the rule are the highest 8 bits of the 16 bit result - that was to be
  2289.     expected since the S flag is extracted from bit 15.
  2290.  
  2291.     Ghosts'n Goblins use the undocumented flag due to a programming error.
  2292.     The rhino in Sabre Wulf walks backward or keeps running in little
  2293.     circles in a corner, if the (in this case undocumented) behaviour of the
  2294.     sign flag in the BIT instruction isn't right.  I quote:
  2295.  
  2296.         AD86    DD CB 06 7E        BIT 7,(IX+6)
  2297.         AD89    F2 8F AD           JP P,#AD8F
  2298.  
  2299.     An amazing piece of code! Speedlock does so many weird things that all
  2300.     must be exactly right for it to run.  Finally, the '128 rom uses the AF
  2301.     register to hold the return address of a subroutine for a while.  To
  2302.     keep all programs happy, and still have a fast emulator, I had to make a
  2303.     compromise.  The undocumented flags are not always emulated right, but
  2304.     they are most of the time.
  2305.  
  2306.     Finally, a remark about the interrupt flip flops IFF1 and IFF2.  There
  2307.     seems to be a little confusion about these.  These flip flops are
  2308.     simultaneously set or reset by the EI and DI instructions.  IFF1
  2309.     determines whether interrupts are allowed, but its value cannot be read.
  2310.     The value of IFF2 is copied to the P/V flag by LD A,I and LD A,R.  When
  2311.     an NMI occurs, IFF1 is reset, thereby disallowing further (maskable)
  2312.     interrupts, but IFF2 is left unchanged.  This enables the NMI service
  2313.     routine to check whether the interrupted program had enabled or disabled
  2314.     maskable interrupts.  So, Spectrum snapshot software can only read IFF2,
  2315.     but most emulators will emulate both, and then the one that matters most
  2316.     is IFF1.
  2317.  
  2318.     Now for the emulated Z80.  I have added eight instructions, to speed up
  2319.     the RS232 input and output of the Interface I and several things of the
  2320.     SamRam.  These opcodes, ED F8 to ED FE are of little use to any other
  2321.     program.  ED FF is a nice one: it returns you to DOS immediately.  I
  2322.     used it for debugging purposes.
  2323.  
  2324.  
  2325.  
  2326. 5.5  File formats
  2327.  
  2328.     This sections describes the formats of the files used by the emulator.
  2329.  
  2330.  
  2331.     ROMS.BIN:
  2332.     ---------
  2333.  
  2334.         00000-03fff     Ordinary Spectrum rom
  2335.         04000-05fff     Interface I rom (8K)
  2336.         06000-09fff     First SamRam rom (contains BASIC)
  2337.         0a000-0dfff     Second SamRam rom (contains monitor,...)
  2338.         0e000-11fff     First Spectrum 128K rom (active at RESET)
  2339.         12000-15fff     Second Spectrum 128K rom (contains BASIC)
  2340.  
  2341.     The ordinary rom has not been modified.  The Interface I rom has
  2342.     undergone some modifications, to speed up the RS232 input/output
  2343.     routines.  If you don't like this, or want to use another version of the
  2344.     Interface I, you could put that code at the right place in the ROMS.BIN
  2345.     file.  The interface I should work properly, although the RS232 will be
  2346.     slower (always FORMAT the "b" or "t" channel at 19200 baud, by the way,
  2347.     if you replace the rom code, there's no point in waiting for nothing!)
  2348.     The microdrive routines have not been modified in any way.  Here are the
  2349.     changes of the Interface I rom:
  2350.  
  2351.         Address: Old: New:           Address: Old: New:
  2352.  
  2353.         0B9E     ED   ED             0D20     FB   00
  2354.         0B9F     5B   FC             0D2A     37   ED
  2355.         0BA0     C3   F5             0D2B     F3   FD
  2356.         0BA1     5C   C3             0D2C     CE   18
  2357.         0BA2     21   34             0D2D     00   10
  2358.         0BA3     20   0C             0D4C     FB   00
  2359.  
  2360.     These changes are not likely to cause problems; there are several
  2361.     versions of the Interface I rom around, and program developers know
  2362.     this.  It is also a bit pointless to check whether the Interface I rom
  2363.     hasn't been modified; who would put his snapshot software in there
  2364.     anyway, and that's what those people are afraid of.
  2365.  
  2366.     The first and second SamRam rom have been modified more extensively. The
  2367.     biggest problem was that switching the upper 32K ram bank is very fast
  2368.     in reality, but on the PC two blocks of 32K bytes had to be REP
  2369.     MOVSWded.  But since no programs know of the SamRam code anyway, this
  2370.     won't cause any more problems it wouldn't already cause either.
  2371.  
  2372.     The two Spectrum 128 roms have not been modified.
  2373.  
  2374.  
  2375.     .TAP FILES:
  2376.     -----------
  2377.  
  2378.     The .TAP files contain blocks of tape-saved data.  All blocks start with
  2379.     two bytes specifying how many bytes will follow (not counting the two
  2380.     length bytes).  Then raw tape data follows, including the flag and
  2381.     checksum bytes.  The checksum is the bitwise XOR of all bytes including
  2382.     the flag byte.  For example, when you execute the line SAVE "ROM" CODE
  2383.     0,2 this will result:
  2384.  
  2385.  
  2386.              |------ Spectrum-generated data -------|       |---------|
  2387.        13 00 00 03 52 4f 4d 7x20 02 00 00 00 00 80 f1 04 00 ff f3 af a3
  2388.  
  2389.        ^^^^^       first block is 19 bytes (17 bytes+flag+checksum)
  2390.              ^^    flag byte (A reg, 00 for headers, ff for datablocks)
  2391.                 ^^ first byte of header, indicating a code block
  2392.  
  2393.        filename    ^^^^^^^^^^^^^
  2394.        header info               ^^^^^^^^^^^^^^^^^
  2395.        checksum of header                          ^^
  2396.        length of second block                         ^^^^^
  2397.        flag byte                                             ^^
  2398.        first two bytes of rom                                  ^^^^^
  2399.        checksum (checkbittoggle would be better)                     ^^
  2400.  
  2401.  
  2402.     The emulator will always start reading bytes at the beginning of a
  2403.     block.  If less bytes are loaded than are available, the other bytes are
  2404.     skipped, and the last byte loaded is used as checksum.  If more bytes
  2405.     are asked for than exist in the block, the loading routine will
  2406.     terminate with the usual tape-loading-error flags set, leaving the error
  2407.     handling to the calling Z80 program.
  2408.  
  2409.     Note that it is possible to join .TAP files by simply stringing them
  2410.     together, for example COPY /B FILE1.TAP + FILE2.TAP ALL.TAP
  2411.  
  2412.     For completeness, I'll include the structure of a tape header.  A header
  2413.     always consists of 17 bytes:
  2414.  
  2415.         Byte    Length  Description
  2416.         0       1       Type (0,1,2 or 3)
  2417.         1       10      Filename (padded with blanks)
  2418.         11      2       Length of data block
  2419.         13      2       Parameter 1
  2420.         15      2       Parameter 2
  2421.  
  2422.     The type is 0,1,2 or 3 for a Program, Number array, Character array or
  2423.     Code file.  A screen$ file is regarded as a Code file with start address
  2424.     16384 and length 6912 decimal.  If the file is a Program file, parameter
  2425.     1 holds the autostart line number (or a number >=32768 if no LINE
  2426.     parameter was given) and parameter 2 holds the start of the variable
  2427.     area relative to the start of the program.  If it's a Code file,
  2428.     parameter 1 holds the start of the code block when saved, and parameter
  2429.     2 holds 32768.  For data files finally, the byte at position 14 decimal
  2430.     holds the variable name.
  2431.  
  2432.  
  2433.     .MDR FILES:
  2434.     -----------
  2435.  
  2436.     The emulator uses a cartridge file format identical to the 'Microdrive
  2437.     File' format of Carlo Delhez' Spectrum emulator Spectator for the QL.
  2438.     The following information is adapted from Carlo's documentation.  It can
  2439.     also be found in the 'Spectrum Microdrive Book', by Ian Logan (co-writer
  2440.     of the excellent 'Complete Spectrum ROM Disassembly').
  2441.  
  2442.     A cartridge file contains 254 'sectors' of 543 bytes each, and a final
  2443.     byte flag which is non-zero is the cartridge is write protected, so the
  2444.     total length is 137923 bytes.  On the cartridge tape, after a GAP of
  2445.     some time the Interface I writes 10 zeros and 2 FF bytes (the preamble),
  2446.     and then a fifteen byte header-block-with-checksum.  After another GAP,
  2447.     it writes a preamble again, with a 15-byte record-
  2448.     descriptor-with-checksum (which has a structure very much like the
  2449.     header block), immediately followed by the data block of 512 bytes, and
  2450.     a final checksum of those 512 bytes.  The preamble is used by the
  2451.     Interface I hardware to synchronise, and is not explicitly used by the
  2452.     software.  The preamble is not saved to the microdrive file:
  2453.  
  2454.     offset length name    contents
  2455.  
  2456.       0      1   HDFLAG   Value 1, to indicate header block
  2457.       1      1   HDNUMB   sector number (values 254 down to 1)
  2458.       2      2            not used
  2459.       4     10   HDNAME   microdrive cartridge name (blank padded)
  2460.      14      1   HDCHK    header checksum (of first 14 bytes)
  2461.  
  2462.      15      1   RECFLG   - bit 0: always 0 to indicate record block
  2463.                           - bit 1: set for the EOF block
  2464.                           - bit 2: reset for a PRINT file
  2465.                           - bits 3-7: not used (value 0)
  2466.      16      1   RECNUM   data block sequence number (value starts at 0)
  2467.      17      2   RECLEN   data block length (<=512, LSB first)
  2468.      19     10   RECNAM   filename (blank padded)
  2469.      29      1   DESCHK   record descriptor checksum (of previous 14 bytes)
  2470.      30    512            data block
  2471.     542      1   DCHK     data block checksum (of all 512 bytes of data
  2472.                            block, even when not all bytes are used)
  2473.     ---------
  2474.     254 times
  2475.  
  2476.  
  2477.     (Actually, this information is 'transparent' to the emulator.  All it
  2478.     does is store 2 times 254 blocks in the .MDR file as it is OUTed,
  2479.     alternatingly of length 15 and 528 bytes.  The emulator does check
  2480.     checksums, see below; the other fields are dealt with by the emulated
  2481.     Interface I software.)
  2482.  
  2483.     A used record block is either an EOF block (bit 1 of RECFLG is 1) or
  2484.     contains 512 bytes of data (RECLEN=512, i.e.  bit 1 of MSB is 1).  An
  2485.     empty record block has a zero in bit 1 of RECFLG and also RECLEN=0.  An
  2486.     unusable block (as determined by the FORMAT command) is an EOF block
  2487.     with RECLEN=0.
  2488.  
  2489.     The three checksums are calculated by adding all the bytes together
  2490.     modulo 255; this will never produce a checksum of 255.  Possibly, this
  2491.     is the value that is read by the Interface I if there's no or bad data
  2492.     on the tape.
  2493.  
  2494.     In normal operation, all first-fifteen-byte blocks of each header or
  2495.     record block will have the right checksum.  If the checksum is not
  2496.     right, the block will be treated as a GAP.  For instance, if you type
  2497.     OUT 239,0 on a normal Spectrum with interface I, the microdrive motor
  2498.     starts running and the cartridge will be erased completely in 7 seconds.
  2499.     CAT 1 will respond with 'microdrive not ready'.  Try it on the
  2500.     emulator...
  2501.  
  2502.  
  2503.     .SCR FILES:
  2504.     -----------
  2505.  
  2506.     .SCR files are memory dumps of the first 6912 bytes of the Spectrum
  2507.     memory.  A coordinate (x,y), x between 0 and 255 and y between 0 and
  2508.     192, (0,0) being the upper left corner of the screen, corresponds to the
  2509.     pixel address
  2510.  
  2511.         16384+INT (x/8)+1792*INT (y/64)-2016*INT (y/8)+256*y
  2512.  
  2513.     I admit this is not quite the clearest way to explain the organization
  2514.     of Spectrum's video memory, but with a bit or (hard) thinking you can
  2515.     extract from above formula all information you need...  The lowest three
  2516.     bits of x determine which bit of this address corresponds to the pixel
  2517.     (x,y).  This bit-map constitutes the larger part of the screen memory,
  2518.     256*192/8=6144 bytes.  The final 768 bytes are attribute bytes.  The
  2519.     address of the attribute byte corresponding to pixel (x,y) is
  2520.  
  2521.         22528+INT (x/8)+32*INT (y/8)
  2522.  
  2523.     The lowest three bits of the attribute byte control the foreground color
  2524.     (the color of the pixel if the corresponding bit in the bitmap is set),
  2525.     bits 3-5 control the background color, bit 6 is the bright bit and bit 7
  2526.     is the flash bit - if it is set, every 16/50th of a second the ULA
  2527.     effectively flips the foreground and background colours.
  2528.  
  2529.  
  2530.  
  2531.     .Z80 FILES:
  2532.     -----------
  2533.  
  2534.     The old .Z80 snapshot format (for version 1.45 and below) looks like
  2535.     this:
  2536.  
  2537.         Byte    Length  Description
  2538.         0       1       A register
  2539.         1       1       F register
  2540.         2       2       BC register pair (LSB, i.e.  C, first)
  2541.         4       2       HL register pair
  2542.         6       2       Program counter
  2543.         8       2       Stack pointer
  2544.         10      1       Interrupt register
  2545.         11      1       Refresh register (Bit 7 is not significant!)
  2546.         12      1       Bit 0  : Bit 7 of the R-register
  2547.                         Bit 1-3: Border colour
  2548.                         Bit 4  : 1=Basic SamRom switched in
  2549.                         Bit 5  : 1=Block of data is compressed
  2550.                         Bit 6-7: No meaning
  2551.         13      2       DE register pair
  2552.         15      2       BC' register pair
  2553.         17      2       DE' register pair
  2554.         19      2       HL' register pair
  2555.         21      1       A' register
  2556.         22      1       F' register
  2557.         23      2       IY register (Again LSB first)
  2558.         25      2       IX register
  2559.         27      1       Interrupt flipflop, 0=DI, otherwise EI
  2560.         28      1       IFF2 (not particularly important...)
  2561.         29      1       Bit 0-1: Interrupt mode (0, 1 or 2)
  2562.                         Bit 2  : 1=Issue 2 emulation
  2563.                         Bit 3  : 1=Double interrupt frequency
  2564.                         Bit 4-5: 1=High video synchronisation
  2565.                                  3=Low video synchronisation
  2566.                                  0,2=Normal
  2567.                         Bit 6-7: 0=Cursor/Protek/AGF joystick
  2568.                                  1=Kempston joystick
  2569.                                  2=Sinclair 1 joystick
  2570.                                  3=Sinclair 2 joystick
  2571.  
  2572.     Because of compatibility, if byte 12 is 255, it has to be regarded as
  2573.     being 1.  After this header block of 30 bytes the 48K bytes of Spectrum
  2574.     memory follows in a compressed format (if bit 5 of byte 12 is one). The
  2575.     compression method is very simple: it replaces repetitions of at least
  2576.     five equal bytes by a four-byte code ED ED xx yy, which stands for "byte
  2577.     yy repeated xx times".  Only sequences of length at least 5 are coded.
  2578.     The exception is sequences consisting of ED's; if they are encountered,
  2579.     even two ED's are encoded into ED ED 02 ED.  Finally, every byte
  2580.     directly following a single ED is not taken into a block, for example
  2581.     ED 6*00 is not encoded into ED ED ED 06 00 but into ED 00 ED ED 05 00.
  2582.     The block is terminated by an end marker, 00 ED ED 00.
  2583.  
  2584.     That's the format of .Z80 files as used by versions up to 1.45.  Since
  2585.     version 2.01 emulates the Spectrum 128 too, there was a need for a new
  2586.     format.
  2587.  
  2588.     The first 30 bytes are almost the same as the old versions' header.  Of
  2589.     the flag byte, bit 4 and 5 have got no meaning anymore, and the program
  2590.     counter (bytes 6 and 7) are zero to signal a version 2.01 .Z80 file.  So
  2591.     loading a new style .Z80 file into an old emulator will cause an error
  2592.     or a reset at the most.
  2593.  
  2594.     After the first 30 bytes, an additional header follows:
  2595.  
  2596.         Byte    Length  Description
  2597.         30      2       Length of additional header block (contains 23)
  2598.         32      2       Program counter
  2599.         34      1       Hardware mode: 0=Spectrum 48K, 1=0+interface I,
  2600.                         2=SamRam, 3=Spectrum 128K, 4=3+interface I.
  2601.         35      1       If in SamRam mode, bitwise state of 74ls259.
  2602.                         For example, bit 6=1 after an OUT 31,13 (=2*6+1)
  2603.                         If in 128 mode, contains last OUT to 7ffd
  2604.         36      1       Contains 0FF if Interface I rom paged
  2605.         37      1       Bit 0: 1 if R register emulation on
  2606.                         Bit 1: 1 if LDIR emulation on
  2607.         38      1       Last OUT to fffd (soundchip register number)
  2608.         39      16      Contents of the sound chip registers
  2609.  
  2610.     Hereafter a number of memory blocks follow, each containing the
  2611.     compressed data of a 16K block.  The compression is according to the old
  2612.     scheme, except for the end-marker, which is now absent.  The structure
  2613.     of a memory block is:
  2614.  
  2615.         Byte    Length  Description
  2616.         0       2       Length of data (without this 3-byte header)
  2617.         2       1       Page number of block
  2618.         3       [0]     Compressed data
  2619.  
  2620.     The pages are numbered, depending on the hardware mode, in the following
  2621.     way:
  2622.  
  2623.         Page    In '48 mode     In '128 mode    In SamRam mode
  2624.  
  2625.          0      48K rom         rom (basic)     48K rom
  2626.          1      Interf. I rom   Interf. I rom   Interf. I rom
  2627.          2      -               rom (reset)     samram rom (basic)
  2628.          3      -               page 0          samram rom (monitor,..)
  2629.          4      8000-bfff       page 1          Normal 8000-bfff
  2630.          5      c000-ffff       page 2          Normal c000-ffff
  2631.          6      -               page 3          Shadow 8000-bfff
  2632.          7      -               page 4          Shadow c000-ffff
  2633.          8      4000-7fff       page 5          4000-7fff
  2634.          9      -               page 6          -
  2635.         10      -               page 7          -
  2636.  
  2637.     In 48K mode, pages 4,5 and 8 are saved.  In SamRam mode, pages 4 to 8
  2638.     are saved.  In 128 mode, all pages from 3 to 10 are saved.  This
  2639.     version saves the pages in numerical order.  There is no end marker.
  2640.     end marker.
  2641.  
  2642.  
  2643.